Arrays
Arrays
Array Types:
An array is a homogeneous aggregate of data elements in which an individual element is
identified by its position in the aggregate, relative to the first element.
The individual data elements of an array are of the same type.
References to individual array elements are specified using subscript expressions.
array_name(subscript_value_list) → element
Subscript Bindings and Array Categories
– The binding of the subscript type to an array variable is usually static, but the
subscript value ranges are sometimes dynamically bound.
integer num_students = get_num_students(); // Assume this function returns the
number of students enrolled
The binding of the subscript type (integer) to the students array variable is static. This
means that the indices used to access elements of the array must be of type integer and
this is determined at compile time.
However, the range of valid indices (0 to num_students - 1) is dynamically bound
based on the number of students enrolled in the class. The size of the array and the
valid indices are determined during runtime based on the value returned by the
get_num_students() function.
So, while the type of subscripts (integer) remains static, the range of valid indices
is dynamically determined based on runtime information.
– There are five categories of arrays, based on the binding to subscript ranges, the
binding to storage, and from where the storage is allocated.
– A static array is one in which the subscript ranges are statically bound and storage
allocation is static (done before run time).
#include <stdio.h>
#define ARRAY_SIZE 5
int main() {
// Declare a static array of integers with a fixed size
int staticArray[ARRAY_SIZE];
return 0;
}
The staticArray is declared with a fixed size of ARRAY_SIZE, which is defined as 5.
The subscript ranges (from 0 to ARRAY_SIZE - 1) are statically bound because they are
determined at compile time based on the constant value of ARRAY_SIZE.
The storage allocation for the staticArray is static and done before runtime. Memory for
all elements of the array is allocated at compile time, and it remains fixed throughout the
execution of the program.
Values are assigned to the elements of the array (staticArray) at runtime.
The array is accessed and its elements are printed using a loop from 0 to ARRAY_SIZE -
1.
The disadvantage is that the storage for the array is fixed for the entire
execution time of the program.
– A fixed stack-dynamic array is one in which the subscript ranges are statically
bound, but the allocation is done at declaration elaboration time during execution.
function processArray()
integer size
input size // get the size of the array from user input
– A stack-dynamic array is one in which both the subscript ranges and the storage
allocation are dynamically bound at elaboration time. Once the subscript ranges are
bound and the storage is allocated, however, they remain fixed during the lifetime of
the variable.
Example:
def create_stack_dynamic_array(size):
# Dynamically allocate memory for the array based on the given size
return stack_dynamic_array
def main():
stack_dynamic_array = create_stack_dynamic_array(size)
# Populate the array with user input
for i in range(size):
for i in range(size):
print(f"stack_dynamic_array[{i}] = {stack_dynamic_array[i]}")
if __name__ == "__main__":
main()
• A heap-dynamic array is one in which the binding of subscript ranges and storage
allocation is dynamic and can change any number of times during the array‘s
lifetime.
• In a heap-dynamic array, the binding of subscript ranges and storage allocation is
dynamic, meaning they can change any number of times during the array's lifetime.
• This flexibility allows for resizing the array as needed. An example of a heap-
dynamic array can be found in languages such as Python, where arrays can be
dynamically resized using the list type.
Array Initialization
Some languages provide the means to initialize arrays at the time their storage is
allocated. An array aggregate for a single-dimensioned array is a list of literals
delimited by parentheses and slashes. For example, we could have
Arrays of strings in C and C++ can also be initialized with string literals. In this
case, the array is one of pointers to characters.
For example,
In Java, similar syntax is used to define and initialize an array of references to String
objects. For example,
Rectangular Array:myArray[3][7]
In this example, myArray is a 2D array with 3 rows and 7 columns. All rows have the same
length (7 elements) and all columns have the same length (3 elements). This creates a grid
where each row is the same length and each column is the same height.
In this example, myArray is a 2D array with 3 rows and 7 columns. All rows have the same
length (7 elements) and all columns have the same length (3 elements). This creates a grid
where each row is the same length and each column is the same height.
MyArray : Rectangular_Array :=
((1, 2, 3, 4, 5, 6, 7),
Jagged Array: In a jagged array, the lengths of rows need not be the same. This means
that different rows can have different numbers of elements.
Jagged Array:
myJaggedArray[3][] = {
{1, 2},
{3, 4, 5},
{6, 7, 8, 9}
In this example, myJaggedArray is a 2D jagged array with 3 rows. The first row has 2 elements,
the second row has 3 elements, and the third row has 4 elements.
Declaration and initialization of a jagged array in Ada
type Jagged_Array is array(1..3) of Array_of_Integers;
type Array_of_Integers is array(Integer range <>) of Integer;
MyJaggedArray : Jagged_Array :=
((1, 2),
(3, 4, 5),
(6, 7, 8, 9));
In this Ada example, MyJaggedArray is a jagged 2D array with 3 rows. Each row is represented
as an array of integers, and the lengths of these arrays can vary, allowing for different numbers
of elements in each row.
Slices:
For example, if A is a matrix, then the first row of A is one possible slice, as are
the last row and the first column. It is important to realize that a slice is not a new data
type. Rather ,it is a mechanism for referencing part of an array as a unit.
If the element type is statically bound and the array is statically bound to
storage, then the value of the constant part can be computed before run time. However,
the addition and multiplication operations must be done at run time.
The generalization of this access function for an arbitrary lower bound is
address (list[k]) = address ( list [lower_bound]) + ( (k - lower_bound) *
element_size)
Suppose we have an array named List with a lower bound of 1, and we want to access the
element at index k. The access function can be generalized as:
Where:
address(list[k]) is the memory address of the element at index k.
address(list[lower_bound]) is the memory address of the element at the lower bound of
the array.
element_size is the size of each element in memory (in bytes).
k is the index we want to access.
lower_bound is the lower bound of the array.
Example in Ada:
Suppose we want to access List(3). Using the access function, we can calculate its memory
address as follows:
= 0x1000 + (2 * 4)
= 0x1000 + 8
= 0x1008
Associative Arrays
%salaries = ("Gary" => 75000, "Perry" => 57000, "Mary" => 55750, "Cedric" =>
47850); Recall that scalar variable names begin with dollar signs ($). For
example,
$salaries {"Perry"} = 58850;
A new element is added using the same assignment statement form. An element
can be removed from the hash with the delete operator, as in
Delete $salaries{"Gary"};