Arrays are a fundamental data structure in ALGOL, allowing you to store and manipulate collections of data efficiently. This section will cover the basics of arrays, including their declaration, initialization, and common operations.

What is an Array?

An array is a collection of elements, all of the same type, stored in contiguous memory locations. Arrays allow you to store multiple values in a single variable, which can be accessed using an index.

Key Concepts:

  • Indexing: Arrays are indexed, meaning each element in the array can be accessed using its position (index) in the array.
  • Homogeneous: All elements in an array are of the same data type.
  • Fixed Size: The size of an array is defined at the time of its declaration and cannot be changed.

Declaring Arrays

In ALGOL, arrays are declared by specifying the type of elements they will hold, followed by the array name and the size of the array in square brackets.

Syntax:

array_name: array [lower_bound:upper_bound] of data_type;

Example:

integer_array: array [1:10] of integer;

This declares an array named integer_array that can hold 10 integers, indexed from 1 to 10.

Initializing Arrays

Arrays can be initialized at the time of declaration or later in the program.

Example:

integer_array: array [1:10] of integer := (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

This initializes integer_array with values from 1 to 10.

Accessing Array Elements

Array elements are accessed using their index. The index is specified in square brackets.

Example:

integer_array[1] := 100;  // Sets the first element to 100
value := integer_array[5];  // Retrieves the value of the fifth element

Common Operations on Arrays

Traversing an Array

Traversing an array means accessing each element of the array, usually to perform some operation on each element.

Example:

for i := 1 step 1 until 10 do
    print(integer_array[i]);

This loop prints each element of integer_array.

Summing Elements of an Array

Example:

sum := 0;
for i := 1 step 1 until 10 do
    sum := sum + integer_array[i];
print(sum);

This code calculates and prints the sum of all elements in integer_array.

Practical Exercise

Exercise 1: Array Initialization and Access

  1. Declare an array of 5 integers.
  2. Initialize the array with values 10, 20, 30, 40, 50.
  3. Print the third element of the array.
  4. Change the value of the second element to 25.
  5. Print all elements of the array.

Solution:

integer_array: array [1:5] of integer := (10, 20, 30, 40, 50);

// Print the third element
print(integer_array[3]);

// Change the value of the second element
integer_array[2] := 25;

// Print all elements
for i := 1 step 1 until 5 do
    print(integer_array[i]);

Exercise 2: Sum of Array Elements

  1. Declare an array of 7 integers.
  2. Initialize the array with values 1, 2, 3, 4, 5, 6, 7.
  3. Write a program to calculate and print the sum of all elements in the array.

Solution:

integer_array: array [1:7] of integer := (1, 2, 3, 4, 5, 6, 7);
sum := 0;

// Calculate the sum of all elements
for i := 1 step 1 until 7 do
    sum := sum + integer_array[i];

// Print the sum
print(sum);

Common Mistakes and Tips

Common Mistakes:

  • Out of Bounds Access: Trying to access an element outside the defined range of the array.
  • Uninitialized Arrays: Using an array without initializing it, leading to undefined behavior.

Tips:

  • Always ensure the array index is within the valid range.
  • Initialize arrays before using them to avoid unexpected results.

Conclusion

In this section, you learned about arrays in ALGOL, including their declaration, initialization, and common operations. Arrays are a powerful tool for managing collections of data, and understanding how to use them effectively is crucial for efficient programming. In the next section, we will explore records, another important data structure in ALGOL.

© Copyright 2024. All rights reserved