C++ Code Example 2: multidimensional array

This code declares a 2-dimensional integer array called “myArray” with 2 rows and 5 columns, and initializes its elements with the values {{0, 1, 2, 3, 4}, {5, 6, 7, 8, 9}}.

In the main function, a nested loop is used to iterate over the rows and columns of the array, and print out each element by accessing it using the index [i][j]. After each inner loop, a newline character is printed to separate the elements in each row.

Finally, the function returns 0, indicating that the program executed successfully.

#include <iostream>
using namespace std;

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

    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 5; j++) {
            cout << myArray[i][j];
        }
        cout << endl;
    }

    return 0;
}
Output
01234
56789