Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 21994

Printing array of ints generates weird output in ada

$
0
0

I created a simple ada program that allows a user to populate an array with a maximum of 100 non negative and non zero integers and then prints them out. When I call the function to print out the numbers it prints them out but at the same time it also prints out a bunch of strange and seemingly random numbers. What mistake have I made in my code that is causing the program to output such strange results? This is my first time writing in ada. For example when I populate the empty array with the numbers [1,2,3,4,5] is prints out this:

    1          2          3          4          5      32624  911328835      32624  911328836      32624   67043328  134217726  134217726 2013265921  134217726  134217726  134217726   67043328  909181968      32624 2114692683      89452  914381552      32624 1543503876          2         14          2         14

I am using the gnatmake compiler on ubuntu and when compiling the source code it doesn't give me any error/warning messages.

Here is my source code, I know that I probably don't need to use seperate functions but I implemented them anyways for learning purposes.

with Ada.Containers.Vectors;with Ada.Text_IO; use Ada.Text_IO;with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;use Ada.Containers;procedure thing is type Vector is array (1..100) of Integer;A: Vector;--array doesn't need to be completely filled upK: Integer;--array filling function belowfunction mkarr return Vector is --asks user to fill empty array with positive and non zero integersbeginAda.Text_IO.Put ("enter numbers to fill array, negative or 0 will stop process: ");for I in 1..100 loop    Ada.Integer_Text_IO.Get(K);    if K>0 then        A(I) := K;    end if;    if K<=0 then        return A;    end if;end loop;return A;end;--array printing prodcedure belowprocedure printarr(A: in out Vector) isbegin    for I in A'Range loop    if A(I)>0 then        Ada.Integer_Text_IO.Put(A(I));        New_Line(1);    end if;    end loop;end printarr;B: Vector := mkarr;--main method belowbeginprintarr(A);end thing;

Viewing all articles
Browse latest Browse all 21994

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>