0% found this document useful (0 votes)
18 views3 pages

652f901ab81f9LAB 3

The document discusses three dimensional arrays in C++. It explains how to declare, initialize and access elements of a 3D array using initializer lists and loops. Declaration is done by specifying the data type and dimensions. Initialization can be done by listing all elements or using nested loops to assign values sequentially.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views3 pages

652f901ab81f9LAB 3

The document discusses three dimensional arrays in C++. It explains how to declare, initialize and access elements of a 3D array using initializer lists and loops. Declaration is done by specifying the data type and dimensions. Initialization can be done by listing all elements or using nested loops to assign values sequentially.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Namal University Mianwali

Department of Computer Science

LAB MANUAL # 3
Subject: DSA Instructor Name: Ammar Ahmad Khan

Code: CS-230 Lab Timings: 02:00 PM to 05:00 PM (Wednesday)

Three Dimensional Arrays

The 3D array is a data structure that stores elements in a three-dimensional


cuboid-like structure. It can be visualized as a collection of multiple two-
dimensional arrays stacked on top of each other. Each element in a 3D array is
identified by its three indices: the row index, column index, and depth index.

Declaration of Three-Dimensional Array in C++

To declare a 3D array in C++, we need to specify its third dimension along with
2D dimensions.

Syntax:
dataType arrayName[d][r][c];

 dataType: Type of data to be stored in each element.


 arrayName: Name of the array
 d: Number of 2D arrays or Depth of array.
 r: Number of rows in each 2D array.
 c: Number of columns in each 2D array.

Initialization of Three-Dimensional Array in 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.

A 3D array in C can be initialized by using:

1. Initializer List
2. Loops

Initialization of 3D Array using Initializer List

Method 1: In this method, we have to write the total number of elements


inside curly braces, and each item is placed at its position according to the
dimension given.

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};

Method 2 (Better): In this method, we have partitioned the elements using


nested lists and it is easy to read.

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} },
};

Initialization of 3D Array using Loops

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);
}
}
}

You might also like