C++ Multi-Dimensional Arrays

A multi-dimensional array is essentially an array of arrays, allowing data to be stored in structured layers.

Tutorials dojo strip



Declaring a Multi-Dimensional Array

Each additional set of square brackets ([][]) defines another dimension for the array.

Example: Two-Dimensional Array

string motorcycleModels[2][4];  

This creates a 2×4 array, meaning:

  • 2 rows (categories)
  • 4 columns (models per category)

Values can be assigned using nested curly braces:

string motorcycleModels[2][4] = {  
    {"CBR600RR", "CB1000R", "Goldwing", "Africa Twin"},  
    {"Ninja ZX-10R", "Z900", "Versys 650", "KX250"}  
};




Three-Dimensional Arrays

Adding another set of brackets [3] creates a three-dimensional array, increasing complexity:

string motorcycleModels[2][2][2] = {  
    {  
        {"CBR600RR", "CB1000R"},  
        {"Goldwing", "Africa Twin"}  
    },  
    {  
        {"Ninja ZX-10R", "Z900"},  
        {"Versys 650", "KX250"}  
    }  
};

With three dimensions, values are organized in layers.




Accessing Multi-Dimensional Array Elements

To retrieve a specific value, reference each dimension’s index:

Example: Getting an Element

#include <iostream>
using namespace std;

int main() {
    string motorcycleModels[2][4] = {  
        {"CBR600RR", "CB1000R", "Goldwing", "Africa Twin"},  
        {"Ninja ZX-10R", "Z900", "Versys 650", "KX250"}  
    };

    cout << motorcycleModels[0][2];  // Outputs "Goldwing"

    return 0;
}

Indexes start at 0, so:

  • [0][2] retrieves the third element (“Goldwing”).




Modifying an Element

Changing an array element follows the same approach:

motorcycleModels[0][0] = "VFR800";  // Updates "CBR600RR" to "VFR800"
cout << motorcycleModels[0][0];  // Outputs "VFR800"




Looping Through a Multi-Dimensional Array

To iterate over a multi-dimensional array, use nested loops.

Example: Iterating Over Two-Dimensional Array

#include <iostream>
using namespace std;

int main() {
    string motorcycleModels[2][4] = {  
        {"CBR600RR", "CB1000R", "Goldwing", "Africa Twin"},  
        {"Ninja ZX-10R", "Z900", "Versys 650", "KX250"}  
    };

    for (int i = 0; i < 2; i++) {  
        for (int j = 0; j < 4; j++) {  
            cout << motorcycleModels[i][j] << "\n";
        }  
    }

    return 0;
}




Looping Through a Three-Dimensional Array

For three dimensions, add another loop:

#include <iostream>
using namespace std;

int main() {
    string motorcycleModels[2][2][2] = {  
        {  
            {"CBR600RR", "CB1000R"},  
            {"Goldwing", "Africa Twin"}  
        },  
        {  
            {"Ninja ZX-10R", "Z900"},  
            {"Versys 650", "KX250"}  
        }  
    };

    for (int i = 0; i < 2; i++) {  
        for (int j = 0; j < 2; j++) {  
            for (int k = 0; k < 2; k++) {  
                cout << motorcycleModels[i][j][k] << "\n";
            }
        }
    }

    return 0;
}




Practical Application of Multi-Dimensional Arrays

Multi-dimensional arrays are useful for structured data storage, such as creating grids, tables, and even game mechanics.

Example: Tracking Stock Availability

bool stockAvailability[4][4] = {  
    {0, 1, 1, 0},  
    {0, 0, 0, 0},  
    {0, 0, 1, 0},  
    {0, 0, 1, 0}  
};

This structure could represent:

  • 0: Out of stock
  • 1: Available

Tutorials dojo strip
Scroll to Top