Arrays in Ada are a fundamental data structure that allows you to store and manipulate collections of elements of the same type. This section will cover the basics of arrays, including their declaration, initialization, and common operations.

Key Concepts

  1. Array Declaration: How to declare arrays in Ada.
  2. Array Initialization: Different ways to initialize arrays.
  3. Accessing Array Elements: How to access and modify elements in an array.
  4. Array Attributes: Useful attributes associated with arrays.
  5. Multidimensional Arrays: Working with arrays that have more than one dimension.

Array Declaration

In Ada, arrays can be declared with a fixed size or can be dynamically sized. Here are the basic forms of array declarations:

Fixed-Size Arrays

type Index_Type is range 1 .. 10;
type Array_Type is array (Index_Type) of Integer;

Dynamic Arrays

type Array_Type is array (Positive range <>) of Integer;

Example

with Ada.Text_IO; use Ada.Text_IO;

procedure Fixed_Array_Example is
   type Index_Type is range 1 .. 5;
   type Array_Type is array (Index_Type) of Integer;
   My_Array : Array_Type := (1, 2, 3, 4, 5);
begin
   for I in Index_Type loop
      Put_Line("Element" & Integer'Image(I) & ": " & Integer'Image(My_Array(I)));
   end loop;
end Fixed_Array_Example;

Array Initialization

Arrays can be initialized in several ways:

Aggregate Initialization

My_Array : Array_Type := (1, 2, 3, 4, 5);

Default Initialization

My_Array : Array_Type := (others => 0);

Example

with Ada.Text_IO; use Ada.Text_IO;

procedure Default_Array_Example is
   type Index_Type is range 1 .. 5;
   type Array_Type is array (Index_Type) of Integer;
   My_Array : Array_Type := (others => 0);
begin
   for I in Index_Type loop
      Put_Line("Element" & Integer'Image(I) & ": " & Integer'Image(My_Array(I)));
   end loop;
end Default_Array_Example;

Accessing Array Elements

You can access and modify elements in an array using the index:

Example

with Ada.Text_IO; use Ada.Text_IO;

procedure Access_Array_Example is
   type Index_Type is range 1 .. 5;
   type Array_Type is array (Index_Type) of Integer;
   My_Array : Array_Type := (1, 2, 3, 4, 5);
begin
   -- Accessing elements
   Put_Line("First Element: " & Integer'Image(My_Array(1)));
   
   -- Modifying elements
   My_Array(1) := 10;
   Put_Line("Modified First Element: " & Integer'Image(My_Array(1)));
end Access_Array_Example;

Array Attributes

Ada provides several useful attributes for arrays:

  • 'First: Returns the first index of the array.
  • 'Last: Returns the last index of the array.
  • 'Length: Returns the number of elements in the array.
  • 'Range: Returns the range of the array indices.

Example

with Ada.Text_IO; use Ada.Text_IO;

procedure Array_Attributes_Example is
   type Index_Type is range 1 .. 5;
   type Array_Type is array (Index_Type) of Integer;
   My_Array : Array_Type := (1, 2, 3, 4, 5);
begin
   Put_Line("First Index: " & Integer'Image(My_Array'First));
   Put_Line("Last Index: " & Integer'Image(My_Array'Last));
   Put_Line("Length: " & Integer'Image(My_Array'Length));
   Put_Line("Range: " & Index_Type'Image(My_Array'Range));
end Array_Attributes_Example;

Multidimensional Arrays

Ada supports multidimensional arrays, which are arrays of arrays.

Example

with Ada.Text_IO; use Ada.Text_IO;

procedure Multidimensional_Array_Example is
   type Index_Type is range 1 .. 3;
   type Matrix_Type is array (Index_Type, Index_Type) of Integer;
   My_Matrix : Matrix_Type := ((1, 2, 3), (4, 5, 6), (7, 8, 9));
begin
   for I in Index_Type loop
      for J in Index_Type loop
         Put(Integer'Image(My_Matrix(I, J)) & " ");
      end loop;
      New_Line;
   end loop;
end Multidimensional_Array_Example;

Practical Exercises

Exercise 1: Sum of Array Elements

Write a program that calculates the sum of all elements in an array.

Solution

with Ada.Text_IO; use Ada.Text_IO;

procedure Sum_Array_Elements is
   type Index_Type is range 1 .. 5;
   type Array_Type is array (Index_Type) of Integer;
   My_Array : Array_Type := (1, 2, 3, 4, 5);
   Sum : Integer := 0;
begin
   for I in Index_Type loop
      Sum := Sum + My_Array(I);
   end loop;
   Put_Line("Sum of Elements: " & Integer'Image(Sum));
end Sum_Array_Elements;

Exercise 2: Transpose of a Matrix

Write a program that calculates the transpose of a 3x3 matrix.

Solution

with Ada.Text_IO; use Ada.Text_IO;

procedure Transpose_Matrix is
   type Index_Type is range 1 .. 3;
   type Matrix_Type is array (Index_Type, Index_Type) of Integer;
   My_Matrix : Matrix_Type := ((1, 2, 3), (4, 5, 6), (7, 8, 9));
   Transposed_Matrix : Matrix_Type;
begin
   for I in Index_Type loop
      for J in Index_Type loop
         Transposed_Matrix(J, I) := My_Matrix(I, J);
      end loop;
   end loop;

   -- Print Transposed Matrix
   for I in Index_Type loop
      for J in Index_Type loop
         Put(Integer'Image(Transposed_Matrix(I, J)) & " ");
      end loop;
      New_Line;
   end loop;
end Transpose_Matrix;

Conclusion

In this section, we covered the basics of arrays in Ada, including their declaration, initialization, and common operations. We also explored multidimensional arrays and provided practical exercises to reinforce the concepts. Understanding arrays is crucial for efficient data manipulation and is a foundational skill for more advanced topics in Ada programming.

© Copyright 2024. All rights reserved