In: Computer Science
Problem 1. (a) Write an Ada declaration for a rectangular two dimensional array of integers named A, assuming that the subscripts will range from 1 to 10 in each dimension.
(b) Write an Ada declaration for a one-dimensional array named A, where each element in the array is a one-dimensional array of integers. Assume that the subscripts for both will range from 1 to 10.
(c) What are the advantages of declaring the array as described in part (a)?
(d) What are the advantages of declaring the array as descibed in part (b)?
a) type A is array(INTEGER range 1..10,
INTEGER range 1..10) of INTEGER;
b) type A2 is array(INTEGER range 1..10) of
INTEGER;
type A3 is array(INTEGER range 1..10) of A2;
c ) and d ) The key is to remember that in second case (case b), each "row" of the array is a separate array object with its own array type; while in the multi-dimensional case (case a), rows don't have their own types.
A sample code to show the difference between the two methods. Look at the way they are accessed in the code.
==== Code ====
with Ada.Text_IO, Ada.Integer_Text_IO;
use Ada.Text_IO,Ada.Integer_Text_IO;
procedure Hello is
type A is array(INTEGER range 1..10,
INTEGER range 1..10) of INTEGER;
type A2 is array(INTEGER range 1..10) of INTEGER;
type A3 is array(INTEGER range 1..10) of A2;
x : A ;
y : A3;
begin
x(1,1) := 1 ;
y(1)(1) := 4;
Put(x(1,1));
Put(y(1)(1));
end Hello;