# Double handling in an array in Ada

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

## 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 on other sites
You should not increment I if you detect a double (since you do not add anything to the array).

##### Share on other sites
Quote:
 Original post by ToohrVykYou 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 on other sites
Quote:
 From wikipediaTo 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 on other sites
How do you suggest I write instead? I´m all confused right now :S

##### 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 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 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 on other sites
Quote:
 Original post by ToohrVykOuch. 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 on other sites
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 ISBEGIN  DOUBLES(INDEX):=CURRENT_NUMBER;END AddNumToArray;PROCEDURE AddIfUnique ISBEGIN  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:BEGINPut_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.

1. 1
Rutin
68
2. 2
3. 3
4. 4
5. 5

• 11
• 10
• 21
• 10
• 33
• ### Forum Statistics

• Total Topics
633438
• Total Posts
3011881
• ### Who's Online (See full list)

There are no registered users currently online

×