Single-Dimensional and Multi-Dimensional Arrays in C++


Arrays are fundamental data structures in C++ that store a fixed-size sequence of elements of the same data type. In C++, arrays can be single-dimensional, storing a simple list of elements, or multi-dimensional, storing elements in multiple dimensions (such as a grid or table). This article explains both types of arrays and provides examples.

1. Single-Dimensional Arrays

A single-dimensional array, also known as a one-dimensional array, is a linear collection of elements. It can be visualized as a list of items in a single row.

Syntax of a Single-Dimensional Array

    data_type array_name[array_size];
        

Example of Single-Dimensional Array

    #include <iostream>

    int main() {
        int numbers[5] = {1, 2, 3, 4, 5};

        std::cout << "Elements of the array: ";
        for (int i = 0; i < 5; i++) {
            std::cout << numbers[i] << " ";
        }

        return 0;
    }
        

In this example:

  • numbers is a single-dimensional array of integers with a size of 5.
  • It stores five integers: 1, 2, 3, 4, and 5.
  • A for loop iterates through the array to print each element.

2. Multi-Dimensional Arrays

Multi-dimensional arrays are arrays of arrays. The most common type is the two-dimensional array, which can be visualized as a matrix with rows and columns. C++ also supports higher dimensions, but each added dimension increases the complexity.

Syntax of a Multi-Dimensional Array

    data_type array_name[size1][size2];
        

Example of a Two-Dimensional Array

    #include <iostream>

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

        std::cout << "Elements of the 2D array (matrix):" << std::endl;
        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:

  • matrix is a two-dimensional array with three rows and three columns.
  • The nested for loops iterate over each row and column to print the matrix elements.

Example of a Three-Dimensional Array

    #include <iostream>

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

        std::cout << "Elements of the 3D array (cube):" << std::endl;
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 2; j++) {
                for (int k = 0; k < 2; k++) {
                    std::cout << cube[i][j][k] << " ";
                }
                std::cout << std::endl;
            }
            std::cout << std::endl;
        }

        return 0;
    }
        

In this example:

  • cube is a three-dimensional array with 2 layers, each containing a 2x2 matrix.
  • Three nested for loops iterate through each dimension to print all elements.

3. Accessing Array Elements

Array elements are accessed by specifying their index. In a multi-dimensional array, you need to specify an index for each dimension.

Example of Accessing Array Elements

    #include <iostream>

    int main() {
        int matrix[2][2] = {{10, 20}, {30, 40}};

        std::cout << "Element at matrix[0][1]: " << matrix[0][1] << std::endl;
        std::cout << "Element at matrix[1][0]: " << matrix[1][0] << std::endl;

        return 0;
    }
        

In this example:

  • matrix[0][1] accesses the element in the first row, second column.
  • matrix[1][0] accesses the element in the second row, first column.

4. Conclusion

Arrays in C++ provide a way to store multiple values of the same type in a structured format. Single-dimensional arrays are ideal for linear data, while multi-dimensional arrays are useful for complex data structures like matrices and tables. Understanding how to work with arrays is essential for handling large data sets and creating organized programs.





Advertisement