Skip to content

Commit 871fc58

Browse files
Resolve "Array chapter improvements"
1 parent b243584 commit 871fc58

File tree

9 files changed

+78
-349
lines changed

9 files changed

+78
-349
lines changed

courses/fundamentals_of_ada/050_array_types/02-constrained_array_types.rst

Lines changed: 15 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -6,146 +6,34 @@ Constrained Array Types
66
Constrained Array Type Declarations
77
-------------------------------------
88

9-
* Syntax
9+
Syntax (simplified)
1010

11-
.. code:: Ada
12-
13-
constrained_array_definition ::=
14-
array index_constraint of subtype_indication
15-
index_constraint ::= (discrete_subtype_definition
16-
{, discrete_subtype_indication})
17-
discrete_subtype_definition ::=
18-
discrete_subtype_indication | range
19-
subtype_indication ::= subtype_mark [constraint]
20-
range ::= range_attribute_reference |
21-
simple_expression .. simple_expression
22-
23-
* Examples
11+
.. container:: latex_environment footnotesize
2412

2513
.. code:: Ada
2614
27-
type Full_Week_T is array (Days) of Float;
28-
type Work_Week_T is array (Days range Mon .. Fri) of Float;
29-
type Weekdays is array (Mon .. Fri) of Float;
30-
type Workdays is array (Weekdays'Range) of Float;
31-
32-
----------------------------------
33-
Multiple-Dimensioned Array Types
34-
----------------------------------
35-
36-
.. container:: columns
37-
38-
.. container:: column
39-
40-
* Declared with more than one index definition
41-
42-
- Constrained array types
43-
- Unconstrained array types
44-
45-
* Components accessed by giving value for each index
46-
47-
.. container:: column
48-
49-
.. container:: latex_environment small
50-
51-
.. code:: Ada
52-
53-
type Three_Dimensioned is
54-
array (
55-
Boolean,
56-
12 .. 50,
57-
Character range 'a' .. 'z')
58-
of Integer;
59-
TD : Three_Dimensioned;
60-
...
61-
begin
62-
TD (True, 42, 'b') := 42;
63-
TD (Flag, Count, Char) := 42;
64-
65-
-----------------------------
66-
Tic-Tac-Toe Winners Example
67-
-----------------------------
68-
69-
.. container:: columns
70-
71-
.. container:: column
72-
73-
.. code:: Ada
74-
75-
-- 9 positions on a board
76-
type Move_Number is range 1 .. 9;
77-
-- 8 ways to win
78-
type Winning_Combinations is
79-
range 1 .. 8;
80-
-- need 3 positions to win
81-
type Required_Positions is
82-
range 1 .. 3;
83-
Winning : constant array (
84-
Winning_Combinations,
85-
Required_Positions)
86-
of Move_Number := (1 => (1,2,3),
87-
2 => (1,4,7),
88-
...
89-
90-
.. container:: column
91-
92-
.. list-table::
93-
:width: 55%
94-
95-
* - :superscript:`1` **X**
96-
97-
- :superscript:`2` **X**
98-
- :superscript:`3` **X**
99-
100-
* - :superscript:`4`
101-
102-
- :superscript:`5`
103-
- :superscript:`6`
104-
105-
* - :superscript:`7`
15+
type <typename> is array (<index constraint>) of <constrained type>;
10616
107-
- :superscript:`8`
108-
- :superscript:`9`
17+
where
10918

110-
* -
19+
.. container:: latex_environment quote
11120

112-
-
113-
-
21+
**typename** - identifier
11422

115-
* - :superscript:`1` **X**
23+
**index constraint** - discrete range or type
11624

117-
- :superscript:`2`
118-
- :superscript:`3`
25+
**constrained type** - type with size known at compile time
11926

120-
* - :superscript:`4` **X**
27+
Examples
12128

122-
- :superscript:`5`
123-
- :superscript:`6`
29+
.. container:: latex_environment footnotesize
12430

125-
* - :superscript:`7` **X**
126-
127-
- :superscript:`8`
128-
- :superscript:`9`
129-
130-
* -
131-
132-
-
133-
-
134-
135-
* - :superscript:`1` **X**
136-
137-
- :superscript:`2`
138-
- :superscript:`3`
139-
140-
* - :superscript:`4`
141-
142-
- :superscript:`5` **X**
143-
- :superscript:`6`
144-
145-
* - :superscript:`7`
31+
.. code:: Ada
14632
147-
- :superscript:`8`
148-
- :superscript:`9` **X**
33+
type Integer_Array_T is array (1 .. 3) of Integer;
34+
type Boolean_Array_T is array (Boolean) of Integer;
35+
type Character_Array_T is array (character range 'a' .. 'z') of Boolean;
36+
type Copycat_T is array (Boolean_Array_T'Range) of Integer;
14937
15038
------
15139
Quiz

courses/fundamentals_of_ada/050_array_types/03-unconstrained_array_types.rst

Lines changed: 12 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Supplying Index Constraints for Objects
4848
.. code:: Ada
4949
5050
Weekend : Schedule := (Sat => 4.0, Sun => 0.0);
51+
-- (Note this is an array aggregate, explained later)
5152
5253
- Further type definitions (shown later)
5354
- Actual parameter to subprogram (shown later)
@@ -232,77 +233,27 @@ Quiz
232233

233234
.. code:: Ada
234235
235-
type Array_T is array (Integer range <>) of Integer;
236-
subtype Array1_T is Array_T (1 .. 4);
237-
subtype Array2_T is Array_T (0 .. 3);
238-
X : Array_T := (1, 2, 3, 4);
239-
Y : Array1_T := (1, 2, 3, 4);
240-
Z : Array2_T := (1, 2, 3, 4);
236+
type Bit_T is range 0 .. 1;
237+
type Bit_Array_T is array (Positive range <>) of Bit_T;
241238
242239
.. container:: columns
243240

244241
.. container:: column
245242

246-
Which statement(s) is (are) legal?
243+
Which declaration(s) is (are) legal?
247244

248-
A. ``X (1) := Y (1);``
249-
B. :answermono:`Y (1) := Z (1);`
250-
C. :answermono:`Y := X;`
251-
D. :answermono:`Z := X;`
245+
A. ``AAA : Array_T (0..99);``
246+
B. :answermono:`BBB : Array_T (1..32);`
247+
C. :answermono:`CCC : Array_T (17..16);`
248+
D. ``DDD : Array_T;``
252249

253250
.. container:: column
254251

255252
.. container:: animate
256253

257254
Explanations
258255

259-
A. :ada:`Array_T` starts at :ada:`Integer'First` not :ada:`1`
260-
B. OK, both in range
261-
C. OK, same type and size
262-
D. OK, same type and size
263-
264-
------
265-
Quiz
266-
------
267-
268-
.. code:: Ada
269-
270-
type My_Array is array (Boolean range <>) of Boolean;
271-
272-
O : My_Array (False .. False) := (others => True);
273-
274-
What is the value of :ada:`O (True)`?
275-
276-
A. :ada:`False`
277-
B. :ada:`True`
278-
C. None: Compilation error
279-
D. :answer:`None: Run-time error`
280-
281-
.. container:: animate
282-
283-
:ada:`True` is not a valid index for :ada:`O`.
284-
285-
NB: GNAT will emit a warning by default.
286-
287-
------
288-
Quiz
289-
------
290-
291-
.. code:: Ada
292-
293-
type My_Array is array (Positive range <>) of Boolean;
294-
295-
O : My_Array (0 .. -1) := (others => True);
296-
297-
What is the value of :ada:`O'Length`?
298-
299-
A. 1
300-
B. :answer:`0`
301-
C. None: Compilation error
302-
D. None: Run-time error
303-
304-
.. container:: animate
305-
306-
When the upper bound is less than the lower bound, this is an empty array.
307-
For empty arrays, the index can be out of range for the index type.
308-
256+
A. :ada:`Array_T` index is :ada:`Positive` which starts at 1
257+
B. OK, indices are in range
258+
C. OK, indicates a zero-length array
259+
D. Object must be constrained

courses/fundamentals_of_ada/050_array_types/05-operations.rst

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,29 @@ Object-Level Operations
2020
2121
* Conversions
2222

23-
.. code:: Ada
24-
25-
C := Foo (B);
26-
2723
- Component types must be the same type
2824
- Index types must be the same or convertible
2925
- Dimensionality must be the same
3026
- Bounds must be compatible (not necessarily equal)
3127

28+
.. code:: Ada
29+
30+
declare
31+
type Index1_T is range 1 .. 2;
32+
type Index2_T is range 101 .. 102;
33+
type Array1_T is array (Index1_T) of Integer;
34+
type Array2_T is array (Index2_T) of Integer;
35+
type Array3_T is array (Boolean) of Integer;
36+
37+
One : Array1_T;
38+
Two : Array2_T;
39+
Three : Array3_T;
40+
41+
begin
42+
43+
One := Array1_T (Two); -- OK
44+
Two := Array2_T (Three); -- Illegal (indices not convertible)
45+
3246
-------------------------------
3347
Extra Object-Level Operations
3448
-------------------------------
@@ -142,23 +156,23 @@ Quiz
142156
143157
type Index_T is range 1 .. 10;
144158
type OneD_T is array (Index_T) of Boolean;
145-
type ThreeD_T is array (Index_T, Index_T, Index_T) of OneD_T;
146-
A : ThreeD_T;
159+
type TwoD_T is array (Index_T) of OneD_T;
160+
A : TwoD_T;
147161
B : OneD_T;
148162
149163
Which statement(s) is (are) legal?
150164

151-
A. :answermono:`B(1) := A(1,2,3)(1) or A(4,3,2)(1);`
152-
B. :answermono:`B := A(2,3,4) and A(4,3,2);`
153-
C. ``A(1,2,3..4) := A(2,3,4..5);``
165+
A. :answermono:`B(1) := A(1,2) or A(4,3);`
166+
B. :answermono:`B := A(2) and A(4);`
167+
C. ``A(1..2)(4) := A(5..6)(8);``
154168
D. :answermono:`B(3..4) := B(4..5)`
155169

156170
.. container:: animate
157171

158172
Explanations
159173

160-
A. All three objects are just Boolean values
174+
A. All objects are just Boolean values
161175
B. An element of :ada:`A` is the same type as :ada:`B`
162-
C. No slicing of multi-dimensional arrays
176+
C. Slice must be of outermost array
163177
D. Slicing allowed on single-dimension arrays
164178

0 commit comments

Comments
 (0)