Write pseudocode for 1D and 2D arrays

Resources | Subject Notes | Computer Science

Arrays (10.2)

Objective

Write pseudo-code for 1D and 2D arrays.

1. One-Dimensional (1D) Arrays

A 1D array is a sequence of elements of the same data type, stored in contiguous memory locations. It's often used to represent lists of items.

Pseudo-code for 1D Array Operations

  1. Declaration: Declare an array of a specified data type and size.
      array [array_name][array_size];
      
  2. Initialization: Assign initial values to the array elements.
      array_name[0] = value;
      array_name[1] = value;
      // ... and so on
      
  3. Accessing Elements: Access an element using its index. Remember that array indices typically start from 0.
      element = array_name[index];
      
  4. Modifying Elements: Change the value of an element at a specific index.
      array_name[index] = new_value;
      
  5. Iteration: Process each element of the array using a loop.
      for i = 0 to array_size - 1 do
        // Perform operation on array_name[i]
      end for
      

Example: Finding the Sum of Elements in a 1D Array

The following pseudo-code calculates the sum of all elements in a 1D array.

Step Description Pseudo-code
1 Declare an array of integers. array[int] [10]
2 Initialize the array with some values. array[0] = 10;
array[1] = 20;
array[2] = 30;
3 Set a variable to store the sum. sum = 0;
4 Iterate through the array. for i = 0 to 9 do
sum = sum + array[i];
end for
5 Output the sum. print sum;

2. Two-Dimensional (2D) Arrays

A 2D array is an array of arrays. It's used to represent tables or matrices, where elements are organized in rows and columns.

Pseudo-code for 2D Array Operations

  1. Declaration: Declare a 2D array specifying the data type, number of rows, and number of columns.
      array [array_name][data_type][number_of_columns];
      
  2. Initialization: Assign initial values to the array elements.
      array_name[row][column] = value;
      
  3. Accessing Elements: Access an element using its row and column indices.
      element = array_name[row_index][column_index];
      
  4. Modifying Elements: Change the value of an element at a specific row and column.
      array_name[row_index][column_index] = new_value;
      
  5. Iteration: Process each element of the array using nested loops. The outer loop iterates through the rows, and the inner loop iterates through the columns.
      for row = 0 to number_of_rows - 1 do
        for column = 0 to number_of_columns - 1 do
          // Perform operation on array_name[row][column]
        end for
      end for
      

Example: Transposing a 2D Array

The following pseudo-code transposes a 2D array, swapping rows and columns.

Step Description Pseudo-code
1 Declare a 2D array of integers. array2d [int] [3][4]
2 Initialize the array with some values. array2d[0][0] = 1;
array2d[0][1] = 2;
array2d[0][2] = 3;
array2d[0][3] = 4;
array2d[1][0] = 5;
array2d[1][1] = 6;
array2d[1][2] = 7;
array2d[1][3] = 8;
array2d[2][0] = 9;
array2d[2][1] = 10;
array2d[2][2] = 11;
array2d[2][3] = 12;
3 Create a new 2D array with swapped dimensions. transposed_array [int] [4][3];
4 Transpose the elements. for i = 0 to 2 do
for j = 0 to 3 do
transposed_array[j][i] = array2d[i][j];
end for
end for
5 Output the transposed array. print transposed_array;

Important Considerations

  • Array indices are typically zero-based.
  • Accessing an element with an invalid index results in an error.
  • The size of the array must be known at compile time (in many languages).
  • Arrays can be dynamically allocated in some languages.