0
0
C++programming~5 mins

Multi-dimensional arrays in C++

Choose your learning style9 modes available
Introduction

Multi-dimensional arrays help you store data in tables or grids, like a chessboard or calendar.

When you want to store data in rows and columns, like a spreadsheet.
When you need to represent a game board, like tic-tac-toe or chess.
When working with images, where pixels are arranged in rows and columns.
When you want to store multiple sets of related data in a structured way.
When you need to perform mathematical operations on matrices.
Syntax
C++
class MultiDimensionalArray {
public:
    int rows;
    int cols;
    int data[3][4];  // fixed size 3 rows and 4 columns

    MultiDimensionalArray() {
        rows = 3;
        cols = 4;
        // Initialize all elements to zero
        for (int row = 0; row < rows; ++row) {
            for (int col = 0; col < cols; ++col) {
                data[row][col] = 0;
            }
        }
    }

    void setValue(int row, int col, int value) {
        if (row >= 0 && row < rows && col >= 0 && col < cols) {
            data[row][col] = value;
        }
    }

    int getValue(int row, int col) {
        if (row >= 0 && row < rows && col >= 0 && col < cols) {
            return data[row][col];
        }
        return -1; // invalid index
    }

    void print() {
        for (int row = 0; row < rows; ++row) {
            for (int col = 0; col < cols; ++col) {
                std::cout << data[row][col] << " ";
            }
            std::cout << std::endl;
        }
    }
};

The first index is the row number, the second is the column number.

Array sizes must be fixed at compile time in this example for simplicity.

Examples
Sets values at the first row, first column and last row, last column, then prints the whole array.
C++
MultiDimensionalArray array;
array.setValue(0, 0, 5);
array.setValue(2, 3, 10);
array.print();
Prints the array when all values are zero (empty).
C++
MultiDimensionalArray emptyArray;
emptyArray.print();
Sets and gets a single element in the middle of the array.
C++
MultiDimensionalArray singleElementArray;
singleElementArray.setValue(1, 2, 7);
std::cout << singleElementArray.getValue(1, 2) << std::endl;
Shows what happens if you try to access outside the array bounds.
C++
MultiDimensionalArray outOfBoundsArray;
outOfBoundsArray.setValue(5, 5, 100); // ignored
std::cout << outOfBoundsArray.getValue(5, 5) << std::endl; // prints -1
Sample Program

This program creates a 3x4 multi-dimensional array, prints it empty, sets some values, prints again, and shows how to get values safely.

C++
#include <iostream>

class MultiDimensionalArray {
public:
    int rows;
    int cols;
    int data[3][4];

    MultiDimensionalArray() {
        rows = 3;
        cols = 4;
        for (int row = 0; row < rows; ++row) {
            for (int col = 0; col < cols; ++col) {
                data[row][col] = 0;
            }
        }
    }

    void setValue(int row, int col, int value) {
        if (row >= 0 && row < rows && col >= 0 && col < cols) {
            data[row][col] = value;
        }
    }

    int getValue(int row, int col) {
        if (row >= 0 && row < rows && col >= 0 && col < cols) {
            return data[row][col];
        }
        return -1;
    }

    void print() {
        for (int row = 0; row < rows; ++row) {
            for (int col = 0; col < cols; ++col) {
                std::cout << data[row][col] << " ";
            }
            std::cout << std::endl;
        }
    }
};

int main() {
    MultiDimensionalArray gameBoard;

    std::cout << "Initial game board:" << std::endl;
    gameBoard.print();

    gameBoard.setValue(0, 0, 1); // top-left corner
    gameBoard.setValue(1, 2, 5); // middle
    gameBoard.setValue(2, 3, 9); // bottom-right corner

    std::cout << "\nGame board after setting some values:" << std::endl;
    gameBoard.print();

    int value = gameBoard.getValue(1, 2);
    std::cout << "\nValue at row 1, column 2: " << value << std::endl;

    int invalidValue = gameBoard.getValue(5, 5);
    std::cout << "Value at invalid position (5,5): " << invalidValue << std::endl;

    return 0;
}
OutputSuccess
Important Notes

Time complexity to access or set an element is O(1) because you use direct indexes.

Space complexity is fixed and depends on the array size (rows * columns).

Common mistake: Trying to access indexes outside the array size causes errors or unexpected results.

Use multi-dimensional arrays when you need fixed-size grids. For dynamic sizes, consider vectors or pointers.

Summary

Multi-dimensional arrays store data in rows and columns, like a table.

You access elements using two indexes: row and column.

They are useful for grids, game boards, and matrices.