Topic 11: Two-Dimensional Arrays

In C++, a two-dimensional array is a collection of variables of the same type arranged in a grid consisting of rows and columns. It can be thought of as an array of arrays.

Declaring and Initializing Two-Dimensional Arrays

To declare a two-dimensional array, specify the type of its elements, followed by the array name and its dimensions in square brackets. Here's an example:

Example: Declaring a Two-Dimensional Array
  • 
    int myArray[3][4];
                                        

You can also initialize a two-dimensional array at the time of declaration:

Example: Initializing a Two-Dimensional Array
  • 
    int myArray[3][4] = {
        {1, 2, 3, 4},
        {5, 6, 7, 8},
        {9, 10, 11, 12}
    };
                                        

Accessing Array Elements

Array elements are accessed using their row and column indices. For example, myArray[0][0] refers to the element in the first row and first column, myArray[1][2] to the element in the second row and third column, and so on.

Example: Accessing Array Elements
  • 
    #include 
    using namespace std;
    
    int main() {
        int myArray[3][4] = {
            {1, 2, 3, 4},
            {5, 6, 7, 8},
            {9, 10, 11, 12}
        };
        cout << "The element at row 1, column 2 is: " << myArray[1][2] << endl;
        cout << "The element at row 0, column 0 is: " << myArray[0][0] << endl;
        return 0;
    }
                                        

Looping Through a Two-Dimensional Array

To process all elements of a two-dimensional array, you can use nested loops. The outer loop iterates through the rows, while the inner loop iterates through the columns:

Example: Looping Through a Two-Dimensional Array
  • 
    #include 
    using namespace std;
    
    int main() {
        int myArray[3][4] = {
            {1, 2, 3, 4},
            {5, 6, 7, 8},
            {9, 10, 11, 12}
        };
        for (int i = 0; i < 3; ++i) {
            for (int j = 0; j < 4; ++j) {
                cout << "Element at row " << i << ", column " << j << " is: " << myArray[i][j] << endl;
            }
        }
        return 0;
    }
                                        

Modifying Array Elements

Array elements can be modified by assigning new values to them using their row and column indices:

Example: Modifying Array Elements
  • 
    #include 
    using namespace std;
    
    int main() {
        int myArray[3][4] = {
            {1, 2, 3, 4},
            {5, 6, 7, 8},
            {9, 10, 11, 12}
        };
        myArray[1][2] = 20;  // Changing the element at row 1, column 2 to 20
        cout << "The new value of the element at row 1, column 2 is: " << myArray[1][2] << endl;
        return 0;
    }
                                        

Practice Questions

  1. What is the difference between a one-dimensional and a two-dimensional array?
  2. What are the advantages of using a two-dimensional array?
  3. Can you store different data types in different elements of a two-dimensional array in C++?
  4. How do you initialize a two-dimensional array to zero given the array declaration int arr[3][3]
  5. What happens if you access an element outside the bounds of a two-dimensional array?
  6. Can you change the number of rows or columns in a two-dimensional array after it has been declared in C++? Why or why not?
  7. How do you calculate the total memory required by a two-dimensional array in C++?
  8. What are some common applications of two-dimensional arrays?
  1. Matrix Addition: Write a C++ program to input two 3x3 matrices from the user and perform matrix addition. Display the resulting matrix.
    • Declare two 3x3 matrices and a third matrix to store the result.
    • Input the elements of both matrices from the user.
    • Add the corresponding elements of both matrices and store them in the result matrix.
    • Display the resulting matrix.
  2. Transpose of a Matrix: Write a C++ program to find the transpose of a 2x3 matrix entered by the user.
    • Declare a 2x3 matrix and a 3x2 matrix for storing the transpose.
    • Input the elements of the 2x3 matrix from the user.
    • Calculate the transpose of the matrix (switch rows and columns).
    • Display the transposed matrix.
  3. Matrix Multiplication: Write a C++ program to multiply two matrices. The first matrix is of size 2x3, and the second matrix is of size 3x2. Display the resulting 2x2 matrix after multiplication.
    • Declare two matrices of size 2x3 and 3x2, respectively.
    • Input the elements of both matrices from the user.
    • Multiply the matrices and store the result in a 2x2 matrix.
    • Display the resulting matrix.
  4. Sum of Rows and Columns: Write a C++ program to calculate the sum of all the elements in each row and each column of a 3x3 matrix entered by the user.
    • Declare a 3x3 matrix.
    • Input the elements of the matrix.
    • Calculate and display the sum of each row and each column.
  5. Finding Maximum and Minimum Elements: Write a C++ program to find the maximum and minimum elements in a 2x3 matrix entered by the user.
    • Declare a 2x3 matrix.
    • Input the elements of the matrix.
    • Traverse the matrix to find the maximum and minimum elements.
    • Display the maximum and minimum values.
  6. Diagonal Sum of a Square Matrix: Write a C++ program to calculate the sum of the diagonal elements of a 3x3 square matrix entered by the user.
    • Declare a 3x3 matrix.
    • Input the elements of the matrix.
    • Calculate the sum of the diagonal elements (from top-left to bottom-right).
    • Display the sum.
  7. Matrix Subtraction: Write a C++ program to input two 2x2 matrices from the user and perform matrix subtraction. Display the resulting matrix.
    • Declare two 2x2 matrices and a third matrix to store the result.
    • Input the elements of both matrices from the user.
    • Subtract the elements of the second matrix from the first and store the result.
    • Display the resulting matrix.
  8. Check if a Matrix is Symmetric: Write a C++ program to check if a 3x3 matrix is symmetric. A matrix is symmetric if it is equal to its transpose.
    • Declare a 3x3 matrix.
    • Input the elements of the matrix.
    • Calculate the transpose of the matrix.
    • Check if the original matrix is equal to its transpose.
    • Display whether the matrix is symmetric or not.
  9. Count Positive and Negative Elements: Write a C++ program to count the number of positive and negative elements in a 3x3 matrix entered by the user.
    • Declare a 3x3 matrix.
    • Input the elements of the matrix.
    • Use loops to count how many elements are positive and how many are negative.
    • Display the counts.
  10. Histogram plotter (Rainfall figures): Write a program that would plot the rainfall chart.
    • Initialize the array with the following sample rainfall figures:
      Book inventory
    • User is required to enter the day and the program will plot the chart. Chart contains the number of * based on the rainfall
    • Example: if day entered is 3 then the chart is **********
  11. Sum elements column by columns: Write a function that returns the sum of all the elements in a specified column in a matrix. Function: double sumColumn(const double m[][SIZE], int rowSize, int columnIndex);
    • Write a test program that reads a 3-by-4 matrix
    • Display the sum of each column.
    • Here is a sample run:
    • Book inventory
Coming Soon...