Ada: Arrays
Table of contents
Arrays are a fundamental data structure in Ada, used to store a collection of elements of the same type. They are versatile and widely used in various programming scenarios.
In Ada, you cannot declare an array without declaring the data type of its elements first. This is because the data type determines the size of each element and the valid range of values that the element can hold. Without specifying the data type, the compiler would not be able to allocate the appropriate amount of memory for the array elements or determine the valid range of values for the elements.
To declare an array in Ada, you follow this syntax:
Code snippet
array_name : array(index_range) of data_type;
Where:
array_name
is the name of the arrayindex_range
specifies the range of indices for the array elementsdata_type
is the type of the array elements
For example, the following declaration creates an array named numbers
that can store 10 integers:
Code snippet
numbers : array(1..10) of Integer;
In this example, the data type of the array elements is Integer
, and the index range is 1..10, which means that the array has 10 elements, indexed from 1 to 10.
Requiring the declaration of the data type before declaring the array helps to enforce type safety and prevents errors. It ensures that the compiler can allocate the correct amount of memory for the array elements and that the elements can only hold values of the specified type. This helps to prevent runtime errors and makes Ada code more reliable.
Declaring Array Type
To declare an array in Ada, you specify the array's name, type of elements, and index range. The index range determines the number of elements and the valid indices for accessing those elements.
Code snippet
type Integer_Array is array(1..10) of Integer;
Here, Integer_Array
is an array type that can store 10 integers, with indices ranging from 1 to 10.
Creating Array Objects
Once you define an array type, you can create array objects using the aggregate syntax:
Code snippet
A : Integer_Array;
This creates an array object named A
of type Integer_Array
, which can hold 10 integers.
Accessing Array Elements
Array elements are accessed using their index values. For example, to access the first element of A
:
Code snippet
Value := A(1);
This assigns the value of A(1)
to the variable Value
.
Initializing Arrays
Arrays can be initialized upon declaration using the aggregate syntax:
Code snippet
B : Integer_Array := (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
This initializes the array B
with the given values.
Multidimensional Arrays
Ada supports multidimensional arrays, where elements are accessed using multiple indices. For instance, a two-dimensional array:
Code snippet
type Matrix is array(1..10, 1..10) of Integer;
This defines a matrix type Matrix
with 10 rows and 10 columns.
Array Operations
Ada provides various operations for manipulating arrays, such as assigning, comparing, and slicing. For example, assigning one array to another:
Code snippet
C := A;
This copies the contents of A
to C
.
Array Parameters
Arrays can be passed as parameters to functions and procedures. The array type is specified in the parameter list:
Code snippet
procedure Add(A, B : Integer_Array);
This procedure takes two integer arrays, A
and B
, as parameters.
Arrays are an essential tool for organizing and managing data in Ada programs. Their flexibility and efficiency make them a valuable asset for various programming tasks.
Note: Arrays in Ada are fixed in size, meaning that the number of elements in an array cannot be changed after it is declared. Once an array is created, its size is fixed, and you cannot add or remove elements from it. This is in contrast to dynamic arrays, which can grow or shrink in size as needed.
The reason arrays in Ada are fixed-size is to ensure efficiency and predictability. By knowing the size of an array at compile time, the compiler can optimize memory allocation and access, making Ada code faster and more predictable in its memory usage. Additionally, fixed-size arrays make it easier to reason about the behavior of Ada programs, as the size of an array is always known and cannot change unexpectedly.
While fixed-size arrays offer these advantages, they also have some limitations. For situations where the amount of data is not known in advance or may change during program execution, dynamic arrays or other data structures may be more suitable.
In summary, arrays in Ada are fixed-size, providing efficiency and predictability, but also limiting flexibility in handling dynamic data growth.
Array Traversal
There are two common ways to traverse an array in Ada: using a for loop or using an iterator.
Using a for loop
The most common way to traverse an array in Ada is to use a for loop. The for loop iterates over the elements of the array, assigning each element to a loop control variable. The loop control variable can then be used to access the current element of the array.
Here is an example of how to traverse an array of integers using a for loop:
Code snippet
for index in 1..10 loop
Put(Item(numbers, index));
end loop;
This code will print the following output:
1
2
3
4
5
6
7
8
9
10
Using an iterator
Ada also supports iterators, which are objects that can be used to traverse collections of data. To use an iterator to traverse an array, you first need to create an iterator for the array. You can do this using the Iterator
function.
Here is an example of how to traverse an array of integers using an iterator:
Code snippet
Iterator numbers_iterator := Iterator(numbers);
loop
if numbers_iterator.HasNext then
Put(numbers_iterator.Next);
else
exit loop;
end if;
end loop;
This code will print the following output:
1
2
3
4
5
Which method should I use?
The method that you use to traverse an array depends on your personal preference. The for loop is a simpler and more concise way to traverse an array, but the iterator is more flexible and can be used to traverse other collections of data, such as linked lists and trees.
Disclaim: This article was created by Bard, not Rix. For some reason, Rix has refused to answer any more questions about Ada. It says he doesn't know this language. Curious, it was forgotten yesterday I have used it.
Subscribe to my newsletter
Read articles from Elucian Moise directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Elucian Moise
Elucian Moise
Software engineer instructor, software developer and community leader. Computer enthusiast and experienced programmer. Born in Romania, living in US.