You can use two-dimensional arrays to represent tables, matrices, etc.
For example, you can represent distances between cities in a table:
Amsterdam | Berlin | London | Madrid | Paris | Rome | Stockholm | |
---|---|---|---|---|---|---|---|
Amsterdam | 0 | 648 | 494 | 1752 | 495 | 1735 | 1417 |
Berlin | 648 | 0 | 1101 | 2349 | 1092 | 1588 | 1032 |
London | 494 | 1101 | 0 | 1661 | 404 | 1870 | 1807 |
Madrid | 1752 | 2349 | 1661 | 0 | 1257 | 2001 | 3138 |
Paris | 495 | 1092 | 404 | 1257 | 0 | 1466 | 1881 |
Rome | 1735 | 1588 | 1870 | 2001 | 1466 | 0 | 2620 |
Stockholm | 1417 | 1032 | 1807 | 3138 | 1881 | 2620 | 0 |
In Ada, this would be represented in a 2-D array:
-- various constants used in data types max_dist : constant := 40077; -- max distance on earth -- type declarations type distances is range 0 .. max_dist; type city is (Amsterdam, Berlin, London, Madrid, Paris, Rome, Stockholm); type distance_table is array (city, city) of distances; -- distances between various European cities inter_city : distance_table := -- Amst, Berl, Lond, Madr, Pari, Rome, Stoc (( 0, 648, 494, 1752, 495, 1735, 1417), -- Amst ( 648, 0, 1101, 2349, 1092, 1588, 1032), -- Berlin ( 494, 1101, 0, 1661, 404, 1870, 1807), -- London (1752, 2349, 1661, 0, 1257, 2001, 3138), -- Madrid ( 495, 1092, 404, 1257, 0, 1466, 1881), -- Paris (1735, 1588, 1870, 2001, 1466, 0, 2620), -- Rome (1417, 1032, 1807, 3138, 1881, 2620, 0)); -- Stock -- distances I have travelled between various cities travelled : distance_table := (others => (others => 0)); your_travel : distance_table;
You need to declare two index types, for the two dimensions
type city is (Amsterdam, Berlin, London, Madrid, Paris, Rome, Stockholm); type distance_table is array (city, city) of distances;
To reference elements of a 2-D array variable, use both index values:
PUT(inter_city(Berlin, Rome)); travelled(Stockholm, London) := 1807;
Nested for loops are often used to process 2D arrays
-- write out the table for from in Amsterdam .. Stockholm loop -- write one line of the table for to in Amsterdam .. Stockholm loop PUT(inter_city(from, to), width=>6); end loop; NEW_LINE; end loop;
You can assign one entire array variable to another of the same type
your_travel := travelled;
An example program shows the use of a 2D array to analyse the hourly temperatures for a week. The two dimensions correspond to the hours in a day, and the days of the week.
In mathematics, we have M row by N column matrices.
Represent them using 2D arrays in Ada
Sometimes they are implemented with constrained array types
type MATRIX is array (POSITIVE range <>, POSITIVE range <>) of INTEGER; P35, Q35, R35 : MATRIX(1..3, 1..5); -- 3 by 5 matrices X24, Y24, Z24 : MATRIX(1..2, 1..4); -- 2 by 4 matrices -- ADD two matrices together function ADD (A, B : in MATRIX) return MATRIX is C : MATRIX (A'RANGE(1), A'RANGE(2)); begin -- ADD for I in A'RANGE(1) loop for J in A'RANGE(2) loop C(I,J) := A(I,J) + B(I,J); end loop; end loop; return C; end ADD; -- call procedure with suitable arrays R35 := ADD(P35, Q35); Z24 := ADD(X24, Y24);