Multi-dimensional Arrays in C++

Multi-dimensional Arrays in C++

In the C++ programming language, a multi-dimensional array is an array structure in the shape of a row-column order matrix within an array. Each element of a multi-dimensional array can be an element value of another array. The most common multi-dimensional arrays in C++ programming are the 2D and 3D array data types. These are essentially a matrix data format view (a grid of rows and columns) as a data type structure. C++ programmers can also create 2-dimensional, 3-dimensional, and even more multi-dimensional arrays in an array program. Remember, the multi-dimensional array data type is rarely used in C++.

Multi-dimensional Arrays in C++

So, let’s explore the concepts of multi-dimensional arrays, 2D, and 3D arrays in C++ programming.

Multi-dimensional array concepts in C++.

In C++ programming, users can declare a multi-dimensional array data type by defining multiple index data storage positions. For example, a 2D array in a C++ program is an array data type in which each array element represents an array structure within itself. A user-defined 3D array can be represented as a view or structure as an “array of arrays of arrays.”

Declaring and initializing a multi-dimensional array in C++.

Two-dimensional array 2D array concepts in C++.

A 2D array data type in C++ programs is an array structure data type within an array of another array data type in row and column order. Two-dimensional arrays in C++ programs are commonly used to represent numeric data values ​​in a matrix order consisting of rows and columns. A 2-dimensional array can be treated as a structured grid with rows and columns.

Syntax for declaring a 2D array in C++.

type array_name[row_size][column_size];

Element of a 2D array.

  • type – This represents the numeric value data type stored in the 2D array. For example, int, float, char, etc.
  • array_name – This is the name of the 2D array being declared in the current program.
  • row_size – This is the number of rows of data values ​​declared in the 2D array.
  • column_size – This is the number of column data values ​​in each row of the 2D array.

Example of declaring and initializing a C++ 2D array.

#include <iostream>

using namespace std;

int main() {

// Here we Declare and initialize a 2D array with 4×4 matrix table view

int matrices[4][4] = {

{10, 20, 30, 40},

{50, 60, 70, 80},

{90, 81, 22, 44},

{28, 99, 11, 33}

};

// here we Accessing and printing 2D array specific data value or elements

cout << “Here 2D Array Index Element at [0][0] – ” << matrics[0][0] << endl; // Result – 10

cout << “Here 2D Array Index Element at [1][1] – ” << matrics[1][1] << endl; // Result – 60

cout << “Here 2D Array Index Element at [2][2] – ” << matrics[2][2] << endl; // Result – 22

cout << “Here 2D Array Index Element at [3][3] – ” << matrics[3][3] << endl; // Result – 33

return 0;

}

2D array array data type explanation.

  • In the above example, a matrix in a 2D array is defined as a 4×4 matrix table (4 rows and 4 columns). Each array element can be accessed and managed using the 2D index method, which defines one array for rows and one for columns.

Accessing elements in a 2D array in C++.

C++ users can access the data values ​​stored in a 2D array element by specifying the index value location in both row and column order in a program.

int matrics[4][4] = {

{10, 20, 30, 40},

{50, 60, 70, 80},

{90, 81, 22, 44},

{28, 99, 11, 33}

};

// Accessing a 2D array element

cout << “Here is the 2D array index element at [0][0] – ” << matrics[0][0] << endl; // These accesses and displays the element in the first row and first column (10) of the 2D array. Result – 10

cout << “Here 2D Array Index Element at [1][2] – ” << matrics[1][1] << endl; // These accesses and displays the element in the second row and third column (70) in the 2D array. Result – 70

Iterating values ​​​​using the looping method on the 2D array data type in C++.

C++ users can use nested loops to iterate over array values ​​in a program using the for, while, and do while loops. The outer loop iterates over the 2D array data type rows sequentially, and the inner loop iterates over the column data type and displays the 2D array data type values ​​on the console screen.

Iterating values ​​using the looping method on the 2D array example.

#include <iostream>

using namespace std;

int main() {

// here we Declare and initialize a 2D array data type value with 4×4 matrix value

int matrices[4][4] = {

{10, 20, 30, 40},

{50, 60, 70, 80},

{90, 81, 22, 44},

{28, 99, 11, 33}

};

// Here we use nested for loops to print the elements of the 2D array data type

for (int p = 0; p < 4; p++) { // Here it loops over the 2D array rows data type

for (int q = 0; q < 4; q++) { // Here it loops over the 2D array columns rows data type

cout << “2D Array Element stored at [” << p << “][” << q << “] = ” << matrices[p][q] << endl;

}

}

return 0;

}

2D array data type explanation with for loop.

  • Here in the above example, a matrix is ​​defined as a 4×4 matrix table (4 rows and 4 columns) in a 2D array. Each array element can be accessed and managed using the for loop 2D index method.

Dynamic initialization concept of 2D arrays in C++.

C++ users can dynamically allocate memory for 2D array values ​​in a program by using the pointer data type special operator, or by using the new keyword reserved for dynamic data memory allocation in a 2D array.

Example Dynamic initialization concept of 2D arrays.

#include <iostream>

using namespace std;

int main() {

int matrows = 4, matcols = 4;

// Here we manually dynamically allocate a 2D array data element with numeric value

int** matricks = new int*[matrows]; // Here we create an array of pointers rows value

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

matricks[i] = new int[matcols]; // here For each 2D array row, create an array column with values

}

// here we manually Initialize 2d array elements values

matrices[0][0] = 10;

matrix[0][1] = 30;

matrices[0][2] = 20;

matrix[0][3] = 40;

matrices[1][0] = 90;

matrices[1][1] = 70;

matrix[1][2] = 60;

matrices[1][3] = 66;

matrices[2][0] = 11;

matrix[2][1] = 12;

matrices[2][2] = 13;

matrices[2][3] = 14;

matrices[3][0] = 21;

matrices[3][1] = 22;

matrices[3][2] = 23;

matrices[3][3] = 44;

// here we Print the list of matrix 2d array elements

for (int p = 0; p < matrows; p++) {

for (int q = 0; q < matcols; q++) {

cout << matrices[p][q] << ” “;

}

cout << endl;

}

//here it Deallocate the 2D array memory

for (int p = 0; p < matrows; p++) {

delete[] matrices[p]; //here it Free each matrix row

}

delete[] matrices; // here it Free the array of pointers location

return 0;

}

2D array data Dynamic initialization explanation.

  • In this example, we dynamically allocate memory values ​​for a 4×4 2D array data type and fill this 2D array data type with the desired values. To avoid memory leak issues with 2D arrays, the memory deallocate method must be implemented.

The concept of multi-dimensional arrays (3D and beyond) in C++.

C++ users can declare or define a multi-dimensional array in a program. A 3D or multi-dimensional array can contain more than two array storage dimension structure view elements. For example, a 3D array data type is a nested array structure data type view of an array within an array. C++ users can think of a multi-dimensional structure as a cube, with each array element having three index structures. For example, a 3D or multi-dimensional array has one “row”, one “column”, and one “depth” representing a 3D multi-dimensional representation.

Syntax of a 3D array in C++.

type array_name[x_size][y_size][z_size];

Element of a 3D array.

  • x_size – This is the number or value of “sheets” or depths in a 3D multi-dimensional array.
  • y_size – This is the number or value of rows in a 3D multi-dimensional array.
  • z_size – This is the number or value of columns in a 3D multi-dimensional array.

Example of declaring and initializing a 3D multi-dimensional array in C++.

#include <iostream>

using namespace std;

int main() {

// here we Declare and initialize a 3D multi-dimensional array 2x4x4 matrix order

int multi3Darray[2][4][4] = {

{

{44, 99, 88,11},

{22, 33, 77, 12},

{90, 20, 30, 10}

},

{

{55, 66, 12, 41},

{95, 76, 59, 53},

{36, 69, 49, 79}

}

};

// here we Accessing elements in a 3D multidimensional array

cout << “Stored Element at 3D array [0][1][3] – ” << multi3Darray[0][1][3] << endl; // Result – 12

cout << “Stored Element at 3D array [1][2][2] – ” << multi3Darray[1][2][2] << endl; // Result – 49

return 0;

}

Iterating over 3D multi-dimensional array data values ​​in C++.

To iterate or traverse through a loop over 3D array data values ​​in a C++ program, a C++ user must use three nested loop variables. One loop iterates over each dimension (depth, row, and column) in the 3D or multi-dimensional array.

Example of Iterating over a 3D multi-dimensional array.

#include <iostream>

using namespace std;

int main() {

// here we Declare and initialize a 3D multi-dimensional array of 2x4x4

int multi3Darray[2][4][4] = {

{

{44, 99, 88,11},

{22, 33, 77, 12},

{90, 20, 30, 10}

},

{

{55, 66, 12, 41},

{95, 76, 59, 53},

{36, 69, 49, 79}

}

};

// here we Iterate over the 3D multi-dimensional array and print their stored elements

for (int p = 0; p < 2; p++) { // here it Loop over depth of 3Darray sheets

for (int q = 0; q < 3; q++) { // here it Loop over 3Darray rows

for (int r = 0; r < 4; r++) { // here it Loop over 3Darray columns

cout << “3D Mult-Dimensional array Element at [” << p << “][” << q << “][” << r << “] – ” << multi3Darray[p][q][r] << endl;

}

}

}

return 0;

}

explanation of Iterating over 3D multi-dimensional array.

  • Here’s a flexible program for storing and manipulating data in multiple dimensions, such as multi-dimensional arrays, matrices, grids, and other structured data. This example helps you represent matrix data in a cube order.

Leave a Reply