TutorChase logo
CIE A-Level Computer Science Notes

10.2.3 Pseudocode for Arrays

Understanding arrays and their manipulation through pseudocode is a cornerstone in computer science education. This section aims to provide a comprehensive guide on defining, initialising, and working with one-dimensional (1D) and two-dimensional (2D) arrays, tailored specifically for A-Level Computer Science students.

Arrays

An array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together. This makes arrays a highly efficient data structure for handling large datasets.

Key Characteristics of Arrays:

  • Homogeneity: All elements in an array are of the same data type.
  • Fixed Size: Once an array is declared, its size cannot be changed.
  • Indexed Elements: Each element in an array is associated with a unique index.

Defining and Initialising 1D Arrays

1D arrays, or single-row arrays, are the simplest form of arrays. They represent data in a linear form.

Steps to Define and Initialise a 1D Array:

  • Declaration: Start by declaring the array with a specific data type and size. The syntax in pseudocode usually follows the pattern: ARRAY arrayName[size] OF dataType.
    • Example: ARRAY numbers[5] OF INTEGER.
  • Initialisation: After declaration, initialise the array either by directly assigning values to each element or using a loop for dynamic assignment.
    • Static Initialisation: numbers = [1, 2, 3, 4, 5].
    • Dynamic Initialisation: Using a FOR loop to assign values based on certain conditions.

Example of Dynamic Initialisation:

Example of Dynamic Initialisation:

This loop initialises the 'numbers' array with the first five multiples of 2.

Defining and Initialising 2D Arrays

2D arrays are akin to matrices, consisting of rows and columns, making them ideal for representing tabular data.

Steps to Define and Initialise a 2D Array:

  • Declaration: Declare the 2D array by specifying the data type and dimensions (rows and columns).
    • Example: ARRAY matrix[3][3] OF INTEGER defines a 3x3 integer array.
  • Initialisation: Use nested loops to initialise each element. The outer loop iterates through rows, and the inner loop through columns.
    • Example: Initialise the matrix with zeroes.

Example of 2D Array Initialisation:

Example of 2D Array Initialisation:

This code snippet sets all elements of the 'matrix' array to zero.

Accessing Array Elements

Accessing elements in an array is straightforward and involves using the index (or indices, in the case of 2D arrays).

Accessing Elements in 1D Arrays:

  • To access the nth element, use the syntax arrayName[n-1].
  • Example: number = numbers[2] retrieves the third element from the 'numbers' array.

Accessing Elements in 2D Arrays:

  • Use row and column indices: arrayName[rowIndex][columnIndex].
  • Example: value = matrix[1][2] retrieves the element from the second row and third column.

Manipulating Array Elements

Manipulation of array elements is crucial for dynamic data handling.

Changing Elements:

  • Directly reassign a new value using the index.
  • Example: numbers[4] = 20 changes the fifth element of 'numbers' to 20.

Iterating Over Arrays:

  • Use a loop to traverse through the array elements.
  • Example: Use a FOR loop to iterate over 'numbers' and increment each element by 1.

Example of Array Iteration:

Example of Array Iteration:

This loop increments each element of the 'numbers' array by 1.

Advanced Techniques in Array Manipulation

Advanced operations on arrays include algorithms for sorting, searching, and multi-dimensional data handling.

Sorting and Searching:

  • Implement common algorithms like bubble sort or linear search using arrays.
  • Pseudocode examples can demonstrate how these algorithms manipulate array data.

Working with Multi-Dimensional Arrays:

  • Operations like matrix addition or transposition can be performed using 2D arrays.
  • Nested loops are commonly used for these operations.

Best Practices and Tips

  • Use Descriptive Names: Choose clear and descriptive names for arrays and indices to enhance readability.
  • Check Array Boundaries: Always ensure that your array indices are within the array bounds to prevent runtime errors.
  • Optimise for Efficiency: Be aware of the implications of your array operations on the time and space complexity, particularly with large arrays.

FAQ

Rotating a 2D array clockwise by 90 degrees involves transposing the array and then reversing the order of elements in each row. Assume you have a square 2D array ARRAY matrix[3][3] OF INTEGER. First, transpose the array: swap matrix[i][j] with matrix[j][i] for all i < j using nested FOR loops. Once transposed, reverse each row. To reverse a row, use a FOR loop running from 0 to half the length of the row, and swap the ith element with the 'length - i - 1' element in the same row. This method ensures that each element moves to its correct position in the rotated array. The process is efficient for square matrices, as it involves in-place swapping without the need for additional memory allocation.

To find the row with the maximum sum in a 2D array, iterate through each row, calculate its sum, and keep track of the row with the highest sum. Start by defining the 2D array, e.g., ARRAY matrix[3][3] OF INTEGER. Initialize variables for storing the maximum sum and the index of the row with this maximum sum. Use a FOR loop to iterate through each row: FOR i = 0 TO 2 DO. Inside this loop, use another FOR loop to sum the elements of the current row. Compare this sum with the current maximum sum, and if it's larger, update the maximum sum and the row index. After completing the iterations, you will have the index of the row with the maximum sum. This approach is efficient as it requires only a single pass through the array, maintaining a constant space complexity while calculating the row sums.

To check for uniqueness of elements in a 1D array, you need to compare each element with every other element. Define and initialise the array, e.g., ARRAY nums[5] OF INTEGER. Use nested FOR loops for comparison: the outer loop FOR i = 0 TO length - 2 DO and the inner loop FOR j = i + 1 TO length - 1 DO. Within the inner loop, check if nums[i] = nums[j]. If this condition is ever true, then there are duplicate elements, and you can conclude that not all elements are unique. If the loops complete without finding any duplicates, then all elements are unique. This method, while straightforward, has a time complexity of O(n^2), which might not be efficient for very large arrays. In such cases, more advanced techniques like using a hash table could be considered for efficiency.

To check if a 2D array is symmetric, you need to compare it with its transpose. In pseudocode, begin by defining a square 2D array, for example, ARRAY matrix[3][3] OF INTEGER. A symmetric matrix means matrix[i][j] is equal to matrix[j][i] for all i, j. Use nested FOR loops to iterate through the array: FOR i = 0 TO 2 DO FOR j = 0 TO i DO. Within the inner loop, check if matrix[i][j] ≠ matrix[j][i]. If this condition is true, then the matrix is not symmetric, and you can exit the loop early. Otherwise, continue until all comparisons are made. If no mismatches are found, the matrix is symmetric. This method efficiently checks for symmetry by comparing each element above the diagonal with its corresponding element below the diagonal, thus reducing the number of comparisons needed.

Reversing the elements of a 1D array involves swapping elements from opposite ends towards the centre. First, define and initialise your array, for instance, ARRAY nums[5] OF INTEGER = [1, 2, 3, 4, 5]. To reverse it, use a FOR loop that runs from 0 to half the length of the array (excluding the middle element in case of an odd length). Inside the loop, swap the ith element with the element at the index 'length - i - 1'. Pseudocode for the swapping mechanism inside the loop would be: temp = nums[i], nums[i] = nums[length - i - 1], nums[length - i - 1] = temp. This technique uses a temporary variable 'temp' to hold one of the values during the swap. By the end of the loop, the array will be reversed. This method is efficient as it iterates only half the length of the array, minimising the number of operations.

Practice Questions

Write pseudocode to define a 1D array named 'temps' that stores 7 temperature values. Then, write pseudocode to calculate the average temperature from this array.

First, define the 1D array 'temps' with 7 elements: ARRAY temps[7] OF REAL. Initialise the array with temperature values, for example, temps = [15.5, 17.2, 16.3, 18.1, 19.0, 20.2, 18.5]. To calculate the average, declare a variable 'sum' initialised to 0. Use a FOR loop to sum the elements: FOR i = 0 TO 6 DO sum = sum + temps[i] ENDFOR. Finally, calculate the average by dividing the sum by 7: average = sum / 7. This pseudocode efficiently computes the average temperature from the 'temps' array.

Consider a 2D array 'grid' of size 3x3, initialised with integer values. Write pseudocode to replace all odd numbers in the array with -1.

First, ensure 'grid' is defined and initialised, e.g., ARRAY grid[3][3] OF INTEGER. Use nested FOR loops to iterate through each element of 'grid': FOR i = 0 TO 2 DO FOR j = 0 TO 2 DO. Within the inner loop, check if the number is odd: IF grid[i][j] MOD 2 ≠ 0 THEN grid[i][j] = -1 ENDIF. Close the loops appropriately. This pseudocode iterates over each element in the 2D array 'grid' and replaces odd numbers with -1, effectively modifying the array to contain only even numbers and -1s where odd numbers were present.

Hire a tutor

Please fill out the form and we'll find a tutor for you.

1/2
About yourself
Alternatively contact us via
WhatsApp, Phone Call, or Email