2d Arrays & Nested Loops

Lesson 24
Author : Afrixi
Last Updated : November, 2022
C++ - Programming Language
This course covers the basics of programming in C++.

In C++, you can use nested loops to traverse and manipulate elements in a 2D array. Here’s an example program that initializes a 2D array with some values, and then prints out the values using nested loops:

#include <iostream>

int main() {
    int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

    // print out the matrix
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            std::cout << matrix[i][j] << " ";
        }
        std::cout << std::endl;
    }

    return 0;
}

In this example, we first create a 2D array matrix with dimensions of 3 by 3, and initialize it with some values. We then use nested for loops to traverse the array, printing out each element. The outer loop iterates over the rows of the matrix, and the inner loop iterates over the columns. We use the loop variables i and j to index into the matrix and access its elements.

The output of the program will be:

1 2 3 
4 5 6 
7 8 9 

You can use similar nested loops to perform various operations on a 2D array, such as finding the maximum or minimum value, calculating the sum or average of its elements, or performing some transformation on its values.

an example of working with a 2D array in C++ using nested loops to iterate over its elements and perform some operations:

#include <iostream>

int main() {
    const int ROWS = 3;
    const int COLS = 4;

    // initialize the array with some values
    int matrix[ROWS][COLS] = {
        {1, 2, 3, 4},
        {5, 6, 7, 8},
        {9, 10, 11, 12}
    };

    // print out the original matrix
    std::cout << "Original matrix:" << std::endl;
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            std::cout << matrix[i][j] << " ";
        }
        std::cout << std::endl;
    }

    // create a second matrix with the same dimensions
    int matrix2[ROWS][COLS];

    // copy the values of the first matrix to the second matrix
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            matrix2[i][j] = matrix[i][j];
        }
    }

    // print out the second matrix to verify the copy
    std::cout << "Copied matrix:" << std::endl;
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            std::cout << matrix2[i][j] << " ";
        }
        std::cout << std::endl;
    }

    return 0;
}

In this example, we start by defining constants for the number of rows and columns in the 2D array. We then create a matrix array with the specified dimensions and initialize it with some values.

Next, we use nested loops to print out the original matrix to the console.

We then create a second matrix2 array with the same dimensions as the first matrix, and use nested loops again to copy the values from the first matrix to the second one.

Finally, we print out the second matrix to verify that the values were copied correctly.

The output of the program will be:

Original matrix:
1 2 3 4 
5 6 7 8 
9 10 11 12 
Copied matrix:
1 2 3 4 
5 6 7 8 
9 10 11 12 

You can modify the program to perform different operations on the matrices, such as adding or subtracting them, multiplying them, or finding the maximum or minimum value in each row or column.