How to Create Array of Arrays in C++

 In C++, creating an array of arrays is a common task especially in scenarios where you need to represent data in a tabular form or work with the matrices. This concept is often referred to as a "two-dimensional array". Understanding how to create and manipulate arrays of arrays is essential for tasks involving complex data structures, graphical representations and numerical computations.

This article will guide you through the steps to create and use arrays of arrays in C++. We will cover both static and dynamic arrays along with the examples.

What is an Array of Arrays?

An array of arrays also known as a two-dimensional array is an array where each element is itself an array. This structure allows you to store data in a matrix form with the rows and columns.

Syntax for Declaring a Two-Dimensional Array

type arrayName[rowSize][columnSize];

  • type: The data type of the elements.
  • arrayName: The name of the array.
  • rowSize: The number of rows in the array.
  • columnSize: The number of columns in the array.

Creating a Static Two-Dimensional Array

A static two-dimensional array has a fixed size determined at compile-time. Here’s how to declare and initialize a static two-dimensional array:

Declaration

int matrix[3][4];

This declaration creates a 3x4 matrix of integers which means 3 rows and 4 columns.

Initialization

You can initialize the array at the time of declaration

int matrix[3][4] = {

{1, 2, 3, 4},

{5, 6, 7, 8},

{9, 10, 11, 12}

};

Alternatively, you can initialize the array elements later:

int matrix[3][4];

matrix[0][0] = 1;

matrix[0][1] = 2;

// ... and so on

Accessing Elements

You can access and modify elements using their indices:

int value = matrix[1][2]; // Accessing element at row 1, column 2

matrix[2][3] = 15; // Modifying element at row 2, column 3

Creating a Dynamic Two-Dimensional Array

A dynamic two-dimensional array allows for more flexibility as its size can be determined at runtime.

Using Pointers

To create a dynamic two-dimensional array you can use pointers and dynamic memory allocation:

Allocation

int rows = 3;

int cols = 4;

int** matrix = new int*[rows]; // Allocate memory for rows

for(int i = 0; i < rows; i++) {

matrix[i] = new int[cols]; // Allocate memory for columns in each row

}

Initialization

You can initialize the elements after allocation:

matrix[0][0] = 1;

matrix[0][1] = 2;

// ... and so on

Accessing Elements

Similar to static arrays elements can be accessed and modified using indices:

int value = matrix[1][2];

matrix[2][3] = 15;

Example :

#include <iostream>

int main() {
    // Static two-dimensional array
    int staticMatrix[3][4] = {
        {1, 2, 3, 4},
        {5, 6, 7, 8},
        {9, 10, 11, 12}
    };
    // Access and print static array elements
    std::cout << "Static Matrix:" << std::endl;
    for(int i = 0; i < 3; i++) {
        for(int j = 0; j < 4; j++) {
            std::cout << staticMatrix[i][j] << " ";
        }
        std::cout << std::endl;
    }
    // Dynamic two-dimensional array
    int rows = 3;
    int cols = 4;
    int** dynamicMatrix = new int*[rows];
    for(int i = 0; i < rows; i++) {
        dynamicMatrix[i] = new int[cols];
    }
    // Initialize dynamic array elements
    for(int i = 0; i < rows; i++) {
        for(int j = 0; j < cols; j++) {
            dynamicMatrix[i][j] = (i + 1) * (j + 1);
        }
    }
    // Access and print dynamic array elements
    std::cout << "Dynamic Matrix:" << std::endl;
    for(int i = 0; i < rows; i++) {
        for(int j = 0; j < cols; j++) {
            std::cout << dynamicMatrix[i][j] << " ";
        }
        std::cout << std::endl;
    }
    // Deallocate dynamic array memory
    for(int i = 0; i < rows; i++) {
        delete[] dynamicMatrix[i];
    }
    delete[] dynamicMatrix;
    return 0;
}

Output :
Static Matrix:
1 2 3 4 
5 6 7 8 
9 10 11 12 
Dynamic Matrix:
1 2 3 4 
2 4 6 8 
3 6 9 12 

Post a Comment

Previous Post Next Post