diff --git a/courses/fundamentals_of_ada/050_array_types/02-constrained_array_types.rst b/courses/fundamentals_of_ada/050_array_types/02-constrained_array_types.rst index a39a6a166..2ab73e8a5 100644 --- a/courses/fundamentals_of_ada/050_array_types/02-constrained_array_types.rst +++ b/courses/fundamentals_of_ada/050_array_types/02-constrained_array_types.rst @@ -6,146 +6,34 @@ Constrained Array Types Constrained Array Type Declarations ------------------------------------- -* Syntax +Syntax (simplified) - .. code:: Ada - - constrained_array_definition ::= - array index_constraint of subtype_indication - index_constraint ::= (discrete_subtype_definition - {, discrete_subtype_indication}) - discrete_subtype_definition ::= - discrete_subtype_indication | range - subtype_indication ::= subtype_mark [constraint] - range ::= range_attribute_reference | - simple_expression .. simple_expression - -* Examples +.. container:: latex_environment footnotesize .. code:: Ada - type Full_Week_T is array (Days) of Float; - type Work_Week_T is array (Days range Mon .. Fri) of Float; - type Weekdays is array (Mon .. Fri) of Float; - type Workdays is array (Weekdays'Range) of Float; - ----------------------------------- -Multiple-Dimensioned Array Types ----------------------------------- - -.. container:: columns - - .. container:: column - - * Declared with more than one index definition - - - Constrained array types - - Unconstrained array types - - * Components accessed by giving value for each index - - .. container:: column - - .. container:: latex_environment small - - .. code:: Ada - - type Three_Dimensioned is - array ( - Boolean, - 12 .. 50, - Character range 'a' .. 'z') - of Integer; - TD : Three_Dimensioned; - ... - begin - TD (True, 42, 'b') := 42; - TD (Flag, Count, Char) := 42; - ------------------------------ -Tic-Tac-Toe Winners Example ------------------------------ - -.. container:: columns - - .. container:: column - - .. code:: Ada - - -- 9 positions on a board - type Move_Number is range 1 .. 9; - -- 8 ways to win - type Winning_Combinations is - range 1 .. 8; - -- need 3 positions to win - type Required_Positions is - range 1 .. 3; - Winning : constant array ( - Winning_Combinations, - Required_Positions) - of Move_Number := (1 => (1,2,3), - 2 => (1,4,7), - ... - - .. container:: column - - .. list-table:: - :width: 55% - - * - :superscript:`1` **X** - - - :superscript:`2` **X** - - :superscript:`3` **X** - - * - :superscript:`4` - - - :superscript:`5` - - :superscript:`6` - - * - :superscript:`7` + type is array () of ; - - :superscript:`8` - - :superscript:`9` +where - * - +.. container:: latex_environment quote - - - - + **typename** - identifier - * - :superscript:`1` **X** + **index constraint** - discrete range or type - - :superscript:`2` - - :superscript:`3` + **constrained type** - type with size known at compile time - * - :superscript:`4` **X** +Examples - - :superscript:`5` - - :superscript:`6` +.. container:: latex_environment footnotesize - * - :superscript:`7` **X** - - - :superscript:`8` - - :superscript:`9` - - * - - - - - - - - * - :superscript:`1` **X** - - - :superscript:`2` - - :superscript:`3` - - * - :superscript:`4` - - - :superscript:`5` **X** - - :superscript:`6` - - * - :superscript:`7` + .. code:: Ada - - :superscript:`8` - - :superscript:`9` **X** + type Integer_Array_T is array (1 .. 3) of Integer; + type Boolean_Array_T is array (Boolean) of Integer; + type Character_Array_T is array (character range 'a' .. 'z') of Boolean; + type Copycat_T is array (Boolean_Array_T'Range) of Integer; ------ Quiz diff --git a/courses/fundamentals_of_ada/050_array_types/03-unconstrained_array_types.rst b/courses/fundamentals_of_ada/050_array_types/03-unconstrained_array_types.rst index 628e90192..b43e2d1a1 100644 --- a/courses/fundamentals_of_ada/050_array_types/03-unconstrained_array_types.rst +++ b/courses/fundamentals_of_ada/050_array_types/03-unconstrained_array_types.rst @@ -48,6 +48,7 @@ Supplying Index Constraints for Objects .. code:: Ada Weekend : Schedule := (Sat => 4.0, Sun => 0.0); + -- (Note this is an array aggregate, explained later) - Further type definitions (shown later) - Actual parameter to subprogram (shown later) @@ -232,23 +233,19 @@ Quiz .. code:: Ada - type Array_T is array (Integer range <>) of Integer; - subtype Array1_T is Array_T (1 .. 4); - subtype Array2_T is Array_T (0 .. 3); - X : Array_T := (1, 2, 3, 4); - Y : Array1_T := (1, 2, 3, 4); - Z : Array2_T := (1, 2, 3, 4); + type Bit_T is range 0 .. 1; + type Bit_Array_T is array (Positive range <>) of Bit_T; .. container:: columns .. container:: column - Which statement(s) is (are) legal? + Which declaration(s) is (are) legal? - A. ``X (1) := Y (1);`` - B. :answermono:`Y (1) := Z (1);` - C. :answermono:`Y := X;` - D. :answermono:`Z := X;` + A. ``AAA : Array_T (0..99);`` + B. :answermono:`BBB : Array_T (1..32);` + C. :answermono:`CCC : Array_T (17..16);` + D. ``DDD : Array_T;`` .. container:: column @@ -256,53 +253,7 @@ Quiz Explanations - A. :ada:`Array_T` starts at :ada:`Integer'First` not :ada:`1` - B. OK, both in range - C. OK, same type and size - D. OK, same type and size - ------- -Quiz ------- - -.. code:: Ada - - type My_Array is array (Boolean range <>) of Boolean; - - O : My_Array (False .. False) := (others => True); - -What is the value of :ada:`O (True)`? - -A. :ada:`False` -B. :ada:`True` -C. None: Compilation error -D. :answer:`None: Run-time error` - -.. container:: animate - - :ada:`True` is not a valid index for :ada:`O`. - - NB: GNAT will emit a warning by default. - ------- -Quiz ------- - -.. code:: Ada - - type My_Array is array (Positive range <>) of Boolean; - - O : My_Array (0 .. -1) := (others => True); - -What is the value of :ada:`O'Length`? - -A. 1 -B. :answer:`0` -C. None: Compilation error -D. None: Run-time error - -.. container:: animate - - When the upper bound is less than the lower bound, this is an empty array. - For empty arrays, the index can be out of range for the index type. - + A. :ada:`Array_T` index is :ada:`Positive` which starts at 1 + B. OK, indices are in range + C. OK, indicates a zero-length array + D. Object must be constrained diff --git a/courses/fundamentals_of_ada/050_array_types/05-operations.rst b/courses/fundamentals_of_ada/050_array_types/05-operations.rst index 7128e2d52..65011c3b1 100644 --- a/courses/fundamentals_of_ada/050_array_types/05-operations.rst +++ b/courses/fundamentals_of_ada/050_array_types/05-operations.rst @@ -20,15 +20,29 @@ Object-Level Operations * Conversions - .. code:: Ada - - C := Foo (B); - - Component types must be the same type - Index types must be the same or convertible - Dimensionality must be the same - Bounds must be compatible (not necessarily equal) + .. code:: Ada + + declare + type Index1_T is range 1 .. 2; + type Index2_T is range 101 .. 102; + type Array1_T is array (Index1_T) of Integer; + type Array2_T is array (Index2_T) of Integer; + type Array3_T is array (Boolean) of Integer; + + One : Array1_T; + Two : Array2_T; + Three : Array3_T; + + begin + + One := Array1_T (Two); -- OK + Two := Array2_T (Three); -- Illegal (indices not convertible) + ------------------------------- Extra Object-Level Operations ------------------------------- @@ -142,23 +156,23 @@ Quiz type Index_T is range 1 .. 10; type OneD_T is array (Index_T) of Boolean; - type ThreeD_T is array (Index_T, Index_T, Index_T) of OneD_T; - A : ThreeD_T; + type TwoD_T is array (Index_T) of OneD_T; + A : TwoD_T; B : OneD_T; Which statement(s) is (are) legal? - A. :answermono:`B(1) := A(1,2,3)(1) or A(4,3,2)(1);` - B. :answermono:`B := A(2,3,4) and A(4,3,2);` - C. ``A(1,2,3..4) := A(2,3,4..5);`` + A. :answermono:`B(1) := A(1,2) or A(4,3);` + B. :answermono:`B := A(2) and A(4);` + C. ``A(1..2)(4) := A(5..6)(8);`` D. :answermono:`B(3..4) := B(4..5)` .. container:: animate Explanations - A. All three objects are just Boolean values + A. All objects are just Boolean values B. An element of :ada:`A` is the same type as :ada:`B` - C. No slicing of multi-dimensional arrays + C. Slice must be of outermost array D. Slicing allowed on single-dimension arrays diff --git a/courses/fundamentals_of_ada/050_array_types/06-looping_over_objects.rst b/courses/fundamentals_of_ada/050_array_types/06-looping_over_objects.rst index dd05f7272..329f51d53 100644 --- a/courses/fundamentals_of_ada/050_array_types/06-looping_over_objects.rst +++ b/courses/fundamentals_of_ada/050_array_types/06-looping_over_objects.rst @@ -96,55 +96,6 @@ Array Component For-Loop Example .. language_version 2012 ----------------------------------------- -For-Loops with Multidimensional Arrays ----------------------------------------- - -.. container:: columns - - .. container:: column - - * Same syntax, regardless of number of dimensions - * As if a set of nested loops, one per dimension - - - Last dimension is in innermost loop, so changes fastest - - * In low-level format looks like - - .. code:: - - for each row loop - for each column loop - print Identity ( - row, column) - end loop - end loop - - .. container:: column - - .. container:: latex_environment small - - .. code:: Ada - - declare - subtype Rows is Positive; - subtype Columns is Positive; - type Matrix is array - (Rows range <>, - Columns range <>) of Float; - Identity : constant Matrix - (1..3, 1..3) := - ((1.0, 0.0, 0.0), - (0.0, 1.0, 0.0), - (0.0, 0.0, 1.0)); - begin - for C of Identity loop - Put_Line (Float'Image (C)); - end loop; - -.. - language_version 2012 - ------ Quiz ------ @@ -152,14 +103,12 @@ Quiz .. code:: Ada declare - type Array_T is array (1..3, 1..3) of Integer + type Array_T is array (1..5) of Integer with Default_Component_Value => 1; A : Array_T; begin - for I in 2 .. 3 loop - for J in 2 .. 3 loop - A (I, J) := I * 10 + J; - end loop; + for I in A'First + 1 .. A'Last - 1 loop + A (I) := I * A'Length; end loop; for I of reverse A loop Put (I'Image); @@ -172,10 +121,10 @@ Quiz Which output is correct? - A. 1 1 1 1 22 23 1 32 33 - B. :answer:`33 32 1 23 22 1 1 1 1` - C. 0 0 0 0 22 23 0 32 33 - D. 33 32 0 23 22 0 0 0 0 + A. 1 10 15 20 1 + B. :answer:`1 20 15 10 1` + C. 0 10 15 20 0 + D. 25 20 15 10 5 .. container:: column diff --git a/courses/fundamentals_of_ada/050_array_types/07-aggregates.rst b/courses/fundamentals_of_ada/050_array_types/07-aggregates.rst index 903bfc16a..6486ef7e1 100644 --- a/courses/fundamentals_of_ada/050_array_types/07-aggregates.rst +++ b/courses/fundamentals_of_ada/050_array_types/07-aggregates.rst @@ -147,42 +147,15 @@ Aggregate Consistency Rules Nested Aggregates ------------------- -* For multiple dimensions * For arrays of composite component types .. code:: Ada - type Matrix is array (Positive range <>, - Positive range <>) of Float; - Mat_4x2 : Matrix (1..4, 1..2) := (1 => (2.5, 3.0), - 2 => (1.5, 0.0), - 3 => (2.1, 0.0), - 4 => (9.0, 0.0)); - ------------------------------ -Tic-Tac-Toe Winners Example ------------------------------ - -.. code:: Ada - - type Move_Number is range 1 .. 9; - -- 8 ways to win - type Winning_Combinations is range 1 .. 8; - -- need 3 places to win - type Required_Positions is range 1 .. 3; - Winning : constant array (Winning_Combinations, - Required_Positions) of - Move_Number := (-- rows - 1 => (1, 2, 3), - 2 => (4, 5, 6), - 3 => (7, 8, 9), - -- columns - 4 => (1, 4, 7), - 5 => (2, 5, 8), - 6 => (3, 6, 9), - -- diagonals - 7 => (1, 5, 9), - 8 => (3, 5, 7) ); + type Col_T is array (1 .. 3) of Float; + type Matrix_T is array (1 .. 3) of Col_T; + Matrix : Matrix_T := (1 => (1.2, 1.3, 1.4), + 2 => (2.5, 2.6, 2.7), + 3 => (3.8, 3.9, 3.0)); ---------------------------------- Defaults Within Array Aggregates @@ -345,11 +318,13 @@ More Information on Iterators Ada 2022 -* You can nest iterators for multiple-dimensioned arrays +* You can nest iterators for arrays of arrays .. code:: Ada - Matrix : array (1 .. 3, 1 .. 3) of Positive := + type Col_T is array (1 .. 3) of Integer; + type Matrix_T is array (1 .. 3) of Col_T; + Matrix : Matrix_T := [for J in 1 .. 3 => [for K in 1 .. 3 => J * 10 + K]]; diff --git a/courses/fundamentals_of_ada/050_array_types/10-lab.rst b/courses/fundamentals_of_ada/050_array_types/10-lab.rst deleted file mode 100644 index a7130b3ae..000000000 --- a/courses/fundamentals_of_ada/050_array_types/10-lab.rst +++ /dev/null @@ -1,50 +0,0 @@ -======== -Lab -======== - ------------ -Array Lab ------------ - -* Requirements - - - Create an array type whose index is days of the week and each element is a number - - Create two objects of the array type, one of which is constant - - Perform the following operations - - + Copy the constant object to the non-constant object - + Print the contents of the non-constant object - + Use an array aggregate to initialize the non-constant object - + For each element of the array, print the array index and the value - + Move part ("source") of the non-constant object to another part ("destination"), and then clear the source location - + Print the contents of the non-constant object - -* Hints - - - When you want to combine multiple strings (which are arrays!) use the concatenation operator (`&`) - - Slices are how you access part of an array - - Use aggregates (either named or positional) to initialize data - ---------------------- -Multiple Dimensions ---------------------- - -* Requirements - - - For each day of the week, you need an array of three strings containing names of workers for that day - - Two sets of workers: weekend and weekday, but the store is closed on Wednesday (no workers) - - Initialize the array and then print it hierarchically - ------------------------------------ -Array Lab Solution - Declarations ------------------------------------ - -.. container:: source_include labs/answers/050_array_types.txt :start-after:--Declarations :end-before:--Declarations :code:Ada :number-lines:1 - -------------------------------------- -Array Lab Solution - Implementation -------------------------------------- - -.. container:: source_include labs/answers/050_array_types.txt :start-after:--Implementation :end-before:--Implementation :code:Ada :number-lines:15 -.. include:: labs/050_array_types.lab.rst - diff --git a/courses/fundamentals_of_ada/labs/050_array_types.lab.rst b/courses/fundamentals_of_ada/labs/050_array_types.lab.rst index 8a753042d..2d5331680 100644 --- a/courses/fundamentals_of_ada/labs/050_array_types.lab.rst +++ b/courses/fundamentals_of_ada/labs/050_array_types.lab.rst @@ -25,9 +25,9 @@ Array Lab - Slices are how you access part of an array - Use aggregates (either named or positional) to initialize data ---------------------- -Multiple Dimensions ---------------------- +------------------ +Arrays of Arrays +------------------ * Requirements diff --git a/courses/fundamentals_of_ada/labs/answers/050_array_types.txt b/courses/fundamentals_of_ada/labs/answers/050_array_types.txt index d03c9ffbc..72553c6d6 100644 --- a/courses/fundamentals_of_ada/labs/answers/050_array_types.txt +++ b/courses/fundamentals_of_ada/labs/answers/050_array_types.txt @@ -11,7 +11,8 @@ procedure Main is Array_Var : Unconstrained_Array_T (Days_Of_Week_T); type Name_T is array (1 .. 6) of Character; - Weekly_Staff : array (Days_Of_Week_T, 1 .. 3) of Name_T; + type Names_T is array (1 .. 3) of Name_T; + Weekly_Staff : array (Days_Of_Week_T) of Names_T; --Declarations --Implementation @@ -41,10 +42,10 @@ begin Wed => ("closed", "closed", "closed"), others => ("Pinky ", "Inky ", "Blinky")); - for Day in Weekly_Staff'Range (1) loop + for Day in Weekly_Staff'Range loop Put_Line (Day'Image); - for Staff in Weekly_Staff'Range (2) loop - Put_Line (" " & String (Weekly_Staff (Day, Staff))); + for Staff of Weekly_Staff(Day) loop + Put_Line (" " & String (Staff)); end loop; end loop; end Main; diff --git a/courses/fundamentals_of_ada/labs/prompts/050_array_types/main.adb b/courses/fundamentals_of_ada/labs/prompts/050_array_types/main.adb index 74ad38919..b2df3d5c4 100644 --- a/courses/fundamentals_of_ada/labs/prompts/050_array_types/main.adb +++ b/courses/fundamentals_of_ada/labs/prompts/050_array_types/main.adb @@ -7,7 +7,8 @@ procedure Main is -- Array2 : ? -- type Name_T is ? (array of characters) - -- Weekly_Staff : ? (2-dimensional array using day and counter as indices) + -- Weekly_Staff : ? (array if an array of Name_T + -- day and counter should be array indices) begin Put_Line ("Array Types");