652f901ab81f9LAB 3
652f901ab81f9LAB 3
LAB MANUAL # 3
Subject: DSA Instructor Name: Ammar Ahmad Khan
To declare a 3D array in C++, we need to specify its third dimension along with
2D dimensions.
Syntax:
dataType arrayName[d][r][c];
To initialize the 3D array in C++, we follow the same methods we have used
to initialize the 2D array. In 3D array, we have one more dimension so we
have to add one more nested list of elements.
1. Initializer List
2. Loops
int x[3][5][2] = {
{ {0, 1}, {2, 3}, {4, 5}, {6, 7}, {8, 9} },
{ {10, 11}, {12, 13}, {14, 15}, {16, 17}, {18, 19} },
{ {20, 21}, {22, 23}, {24, 25}, {26, 27}, {28, 30} },
};
This method is the same as initializing a 2D array using loops with one more
nested loop for the third dimension.
int x[3][5][2];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 5; j++) {
for (int k = 0; k < 2; k++) {
x[i][j][k] = (some_value);
}
}
}