• Advertisement
Sign in to follow this  

Double handling in an array in Ada

This topic is 4146 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I am working on an array in the programming language Ada where you insert 10 integers into an array with ten positions in it. When you insert one or more doubles, the program should be able to handle it by writing out each one of the doubles. It should look like this: Insert a series of interests between 1 and 15, max 10 pieces. Finish by 0: 5 10 5 10 0 Output: 5 10 I get like this: Insert a series of interests between 1 and 15, max 10 pieces. Finish by 0: 5 10 5 10 0 Output: 5 10 10 I manage to handle one double but if I insert another double the program can´t handle it but instead writes out the both 10:s. How do you write it correctly? The code is below here: WITH Ada.Text_IO, Ada.Integer_Text_IO; USE Ada.Text_IO, Ada.Integer_Text_IO; PROCEDURE Array IS ---TYPE Integer_array IS ARRAY (1 .. 10) OF Integer; ---H : Integer_array := (OTHERS => 0); ---I, ---Number : Integer; BEGIN ---Put_Line("Insert a series of interests between 1 and 15, max 10 pieces."); ---Put("Finish by 0: "); ---Number := 1; ---I := 1; ---WHILE Number > 0 LOOP ------Get(Number); ------FOR K IN 1 .. I LOOP ---------IF H(K) = Number THEN ------------EXIT; ---------ELSE ------------H(I):= Number; ---------END IF; ------END LOOP; ------I := I + 1; ---END LOOP; ---Put_Line("Output:"); ---FOR J IN 1 .. 10 LOOP ---------IF H(J) > 0 THEN ------------Put(H(J), Width=>3); ---------END IF; ---END LOOP; END Array;

Share this post


Link to post
Share on other sites
Advertisement
You should not increment I if you detect a double (since you do not add anything to the array).

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
You should not increment I if you detect a double (since you do not add anything to the array).

Increment? What do you mean by that?

Share this post


Link to post
Share on other sites
Quote:
From wikipedia
To increment a quantity may be, if not otherwise specified, to add 1 to it.


I am specifically referring to the I := I+1; line.

Share this post


Link to post
Share on other sites
---WHILE Number > 0 LOOP
------Get(Number);
------FOR K IN 1 .. I LOOP
---------IF H(K) = Number THEN
------------EXIT;
---------ELSE
------------H(I):= Number;
------------I := I + 1;
---------END IF;
------END LOOP;
---END LOOP;

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
---WHILE Number > 0 LOOP
------Get(Number);
------FOR K IN 1 .. I LOOP
---------IF H(K) = Number THEN
------------EXIT;
---------ELSE
------------H(I):= Number;
------------I := I + 1;
---------END IF;
------END LOOP;
---END LOOP;


It is still the same, unfortunaly :( I get the same thing.

Share this post


Link to post
Share on other sites
Ouch. It appears that I am, in fact, completely stupid.
You are currently looping through your array, and writing the element to the array as soon as you find an element with a different value. You need to check that there is no element in the array with the same value before writing the element. So a loop first, and then a modification if the loop concluded that there are no doubles.

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
Ouch. It appears that I am, in fact, completely stupid.
You are currently looping through your array, and writing the element to the array as soon as you find an element with a different value. You need to check that there is no element in the array with the same value before writing the element. So a loop first, and then a modification if the loop concluded that there are no doubles.


How do I write that?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Just in case this should be any sort of urgent homework, you might wanna play with the following snippet of code:


WITH Ada.Text_IO, Ada.Integer_Text_IO;
USE Ada.Text_IO, Ada.Integer_Text_IO;

PROCEDURE DoubleInput IS
TYPE DOUBLE_ARRAY is ARRAY(1..10) OF Integer;

DOUBLES : DOUBLE_ARRAY := (OTHERS => 0);
CURRENT_NUMBER : Integer := 1;
INDEX : Integer :=1;
INPUT_ERROR : BOOLEAN :=false;

FUNCTION isUnique return BOOLEAN IS
isNotFound : BOOLEAN :=true;
BEGIN
FOR I IN 1..INDEX LOOP
IF DOUBLES(I)=CURRENT_NUMBER THEN isNotFound:=false;
END IF;
END LOOP;
RETURN isNotFound;
END isUnique;

PROCEDURE AddNumToArray IS
BEGIN
DOUBLES(INDEX):=CURRENT_NUMBER;
END AddNumToArray;

PROCEDURE AddIfUnique IS
BEGIN
if isUnique then AddNumToArray;
end if;
END AddIfUnique;

PROCEDURE ShowOutput IS
value : Integer :=0;
BEGIN
FOR I IN 1 .. INDEX-1 LOOP
value := Doubles(I);
if value > 0 THEN Put(I); Put(value); New_Line; end if ;
END LOOP;
END ShowOutput;

-- entry point:
BEGIN
Put_Line("Insert a series of interests between 1 and 15, max 10 pieces.");
Put("Finish by 0: ");New_Line;

FOR I IN 1..10 LOOP
<<again>>
CURRENT_NUMBER :=0;
Put("Item #");Put(INDEX);Put(":");New_Line;

if INPUT_ERROR THEN Put("Sorry, only values from 1-15 are acceptable-Try again please:");New_Line;INPUT_ERROR:=false;END IF;
Get(CURRENT_NUMBER);
if CURRENT_NUMBER < 0 OR CURRENT_NUMBER > 15 THEN INPUT_ERROR:=true; goto again;END IF;

IF CURRENT_NUMBER=0 THEN EXIT; END IF;
AddIfUnique;
INDEX:=INDEX+1;
END LOOP;
Put("Output:"); Put(INDEX); New_Line;
ShowOutput;
END DoubleInput;




...next time, post the complete assignment.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
This really looks a lot like homework and the descriptions in the original posting are pretty ambiguous: you could either be referring to storing DOUBLES (that is NON-integers!) in an array, or storing values in "pairs" (doubles?), or possibly extracting multiple values from a single input string:

So, given the example code and I/O you provided I suggest you look into splitting up a string using custom delimeters.
Basically, it will work pretty much like this:


//pseudo code:
get(inputstring);
a=count(delimeter,inputstring); //find number of occurances of delimeter in inputstring
if a>0 {
//foreach substring based on next delimeter position and last position, determine type of data (i.e. integer) and save it to the array.
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement