Multidimensional arrays syntax

Multidimensional arrays are created by specifying two or more pairs of square brackets in the declaration. Multi-dimensional arrays are created as arrays of arrays. The initialization takes place in the same way as one-dimensional arrays by specifying the number of elements per dimension. Multi-dimensional arrays can be accessed by specifying all required indices, each in their own square brackets. Even with multi-dimensional arrays, a literal initialization can be achieved by nesting the initialization sequences.

Declaration

type array_name[size1][size2];
  • size1, size2: Sizes of the first and second dimensions.

Example

int matrix[3][4];
  • Declares a 2D array matrix with 3 rows and 4 columns.

Initialization

type array_name[size1][size2] = {{row1_values}, {row2_values}, ..., {rowN_values}};
  • row1_values, row2_values, …, rowN_values: Values for each row.

Example

int matrix[3][4] = {
    {1, 2, 3, 4},
    {5, 6, 7, 8},
    {9, 10, 11, 12}
};
  • Initializes a 3×4 matrix.

Complete Example Program

#include <iostream>
using namespace std;

int main() {
    // Single-dimensional array
    int numbers[5] = {1, 2, 3, 4, 5};
    
    // Accessing and printing elements
    for (int i = 0; i < 5; i++) {
        cout << "Element at index " << i << ": " << numbers[i] << endl;
    }
    
    // Multi-dimensional array
    int matrix[3][4] = {
        {1, 2, 3, 4},
        {5, 6, 7, 8},
        {9, 10, 11, 12}
    };
    
    // Accessing and printing elements
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 4; j++) {
            cout << "Element at [" << i << "][" << j << "]: " << matrix[i][j] << endl;
        }
    }
    
    return 0;
}

Code Explanation

Header Inclusion

#include <iostream>
  • This includes the standard input-output stream library, allowing the use of std::cout for console output.

Namespace Declaration

using namespace std;
  • This allows direct access to standard library objects and functions without prefixing them with std::.

Main Function

int main() {
  • The main function is the starting point of the program.

Single-Dimensional Array Declaration and Initialization

int numbers[5] = {1, 2, 3, 4, 5};
  • Declares an integer array numbers with 5 elements and initializes it with the values {1, 2, 3, 4, 5}.

Accessing and Printing Elements of Single-Dimensional Array

for (int i = 0; i < 5; i++) {
    cout << "Element at index " << i << ": " << numbers[i] << endl;
}
  • For Loop Initialization: int i = 0 initializes the loop counter i to 0.
  • Condition: i < 5 runs the loop as long as i is less than 5.
  • Increment: i++ increments i by 1 after each iteration.
  • Loop Body: cout << "Element at index " << i << ": " << numbers[i] << endl; prints the value of numbers[i] at each index i.

Multi-Dimensional Array Declaration and Initialization

int matrix[3][4] = {
    {1, 2, 3, 4},
    {5, 6, 7, 8},
    {9, 10, 11, 12}
};
  • Declares a 2D integer array matrix with 3 rows and 4 columns, initializing it with the values provided.

Accessing and Printing Elements of Multi-Dimensional Array

for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 4; j++) {
        cout << "Element at [" << i << "][" << j << "]: " << matrix[i][j] << endl;
    }
}
  • Outer Loop Initialization: int i = 0 initializes the outer loop counter i to 0.
  • Outer Loop Condition: i < 3 runs the outer loop as long as i is less than 3.
  • Outer Loop Increment: i++ increments i by 1 after each iteration.
  • Inner Loop Initialization: int j = 0 initializes the inner loop counter j to 0