Initialization of Multidimensional Arrays in C++
Last Updated :
12 Feb, 2024
In C++, multidimensional arrays are the type of arrays that have multiple dimensions, i.e., they can expand in multiple directions. In this article, we will discuss how to initialize the multidimensional arrays in C++.
Methods to Initialize Multidimensional Array in C++
We can initialize multidimensional arrays in C++ using the following ways:
- Initialization using Initializer List
- Initialization with Default Values
1. Initialization Using Initializer List
We can initialize the multidimensional arrays using the list initialization as shown:
Initializing 2D Arrays
arr[2][3] = { {1, 2, 3}, {4, 5, 6} };
Initializing 3D Arrays
arr[2][3][2] = { { {1, 2}, {3, 4}, {5, 6} }, { {7, 8}, {9, 10}, {11, 12} } };
Example
C++
// C++ program to illustrate how to initialize the
// multidimensional array
#include <iostream>
using namespace std;
int main()
{
// Declaring a 2D array with 2 rows and 3 columns
int arr[2][3] = { { 1, 2, 3 }, { 4, 5, 6 } };
// Declaring a 3D array with 2 rows and 3 columns and 2
// depth
int arr1[2][3][2] = { { { 1, 2 }, { 3, 4 }, { 5, 6 } },
{ { 7, 8 }, { 9, 10 }, { 11, 12 } } };
// printing 2d array
cout << "2D Array: ";
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
cout << arr[i][j] << " ";
}
}
cout << endl;
// printing 2d array
cout << "3D Array: ";
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 2; k++) {
cout << arr1[i][j][k] << " ";
}
}
}
return 0;
}
Output2D Array: 1 2 3 4 5 6
3D Array: 1 2 3 4 5 6 7 8 9 10 11 12
2. Initialization with Zero
In C++, we can initialize the multidimensional array with a zero in a single statement.
Syntax
arr[2][3] = {0}
Here, all the elements will be initialized with the value zero.
Example
C++
// C++ program to initialize 2D array with zero
#include <iostream>
using namespace std;
int main()
{
// Declare and initialize a 2D array with 2 rows and 3
// columns
int array[2][3] = { 0 };
// Output the values
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 3; ++j) {
// Output each element of the array
cout << array[i][j] << " ";
}
// Move to the next line after printing each row
cout << endl;
}
return 0;
}
Conclusion
In summary, initializing multidimensional arrays in C++ involves setting them up with initial values(Providing the values in code itself). There are few different method of initialization discussed in this article. We can choose the relevant method according to our choice.
Similar Reads
Initialization of Multidimensional Array in C In C, multidimensional arrays are the arrays that contain more than one dimensions. These arrays are useful when we need to store data in a table or matrix-like structure. In this article, we will learn the different methods to initialize a multidimensional array in C. The easiest method for initial
4 min read
How to iterate a Multidimensional Array? Multidimensional arrays are arrays that have more than one dimension. For example, a simple array is a 1-D array, a matrix is a 2-D array, and a cube or cuboid is a 3-D array but how to visualize arrays with more than 3 dimensions, and how to iterate over elements of these arrays? It is simple, just
8 min read
C++ Multidimensional Array A multidimensional array is an array with more than one dimension. It means that it can grow in different directions i.e. instead of changing the length only, it can also change in width, depth or more.Syntax of Multidimensional ArraysC++data_type array_name[s1][s2]...[sn];where s1, s2,â¦, sn are the
8 min read
How to print dimensions of multidimensional array in C++ Create a function that prints multidimensional array size by dimensions i.e.:Examples: Input : int a[2][3][4]; printDimensions(a); Output : 2x3x4 Input : int b[5][6]; printDimensions(a); Output : 5x6 To solve this problem we should use template function to figure out the size of current array. Then
2 min read
How to Get Value of Multidimensional Array in C? Prerequisite: Array in C An array is a type of data structure where we can store multiple elements of similar data types. A multidimensional array can be termed an array of arrays that stores homogeneous data in tabular form. Data in multidimensional arrays are stored in row-major order. Declaration
8 min read