2

I have an assignment where I do not know the number of entries there will be prior to getting them from a user. In this assignment, I am to use unbounded arrays. Once the user states this maximum number, it will be used as the upper bound of the array, and there will be three of these declarations. Then, I need to accept the value to fill each of tose arrays that will be held in a record, which will be held in another array. Then, once populated, The array needs to be passed to another procedure to then output to screen.

I have put together a demo of what needs to happen. This is not the assignment, but just a rough idea/pseudo of one small spart of the larger assignment. I am right away running into the problem of using an unconstrained array in a record.

WITH Ada.Text_IO; USE Ada.Text_IO;
WITH Ada.Integer_Text_Io; USE Ada.Integer_Text_Io;

    PROCEDURE Vehicles IS
       TYPE Year_Array IS ARRAY (Positive RANGE <>) OF Integer;
       TYPE Vehicle_Record IS
          RECORD
             Name: String (1 .. 40);
             Cars_Array : Year_Array;
             Trucks_Array: Year_Array;
             Bikes_Array : Year_Array;
          END RECORD;
       TYPE O_Array IS ARRAY (1 .. 4) OF Vehicle_Record;

       PROCEDURE Get_No_Vehicles (Cars, Trucks, Bikes :    OUT Positive) IS
       BEGIN
          Put("Total number of cars:");
          Get(Cars);
          Put("Total number of trucks:");
          Get(Trucks);
          Put("Total number of bikes:");
          Get(Bikes);
       END Get_No_Vehicles;

       Owner   : O_Array;
       Cars, Trucks, Bikes : Positive;
    BEGIN
       Get_No_Vehicles(Cars, Trucks, Bikes);
    -- Call a procedure that will get the the owner name and the year of each vehicle for all 4 owners.
    -- Call a procedure that will pull the records from the owner array and output to screen.
    END Vehicles;

Does anyone have a suggestion on how to make this work? I thought about creating the record in a declare block, but it will go out of scope when I before I can pass it to the procedure that will output the entire array.I am not sure how to proceed. Thanks for taking the time to look.

user2297683
  • 406
  • 1
  • 5
  • 15

1 Answers1

-1

I have a similar issue on declaring a variable of an unbounded array type, where I want to pass the range actuals when declaring the variable of the boxed array. In your case you are declaring 3 variables of the type Year_array [which is unbounded], but you are not setting the range in cars_array, trucks_array or Bikes_array

Cars_Array : Year_Array; Trucks_Array: Year_Array; `Bikes_Array : Year_Array;

If you know the range for Cars, Trucks, and Bikes array, you should do like this: enter code hereCars_array :Years_array(1..25); enter code heretrucks_aray: Years_array (1..15); ---

I am not sure your code would compile; but in any case somewhere you must put in actual range values in your instance of the unbounded array type.

Booch, G., Bryan, D. (p. 108) Software Engineering with Ada. 3rd Ed. 1994. Benjamin/Cummings Publishing. Redwood City, California.