0% found this document useful (0 votes)
13 views4 pages

Include

Uploaded by

Qazi Sulal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views4 pages

Include

Uploaded by

Qazi Sulal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

OOP

Assignment: 01

Submitted To:
Sir Tariq Javed
Submitted By:
Jamshaid Iqbal
Class:
14-A
Roll No:
F-4297
Q: Write a C++ Program for Three Dimensional Array and also display the output.

#include<stdio.h>
#include<conio.h>

void main()
{
int i, j, k;
int a[3][3][3]=
{
{
{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}
},
};
clrscr();
printf("3D Array Elements\n\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
for(k=0;k<3;k++)
{
printf("%d\t",a[i][j][k]);
}
printf("\n");
}
printf("\n");
}
getch();
}
OUTPUT ON TurboC++

Output on Turbo C++:

In the code above we have declared a multidimensional integer array named “arr” which can hold
3x3x3 (or 27) elements.

We have also initialized the multidimensional array with some integer values.

As I said earlier, a 3D array is an array of 2D arrays. I have divided elements accordingly for easy
understanding. Looking at the C code sample above,

 In lines 9-13, 14-18, and 19-23, each block is a 2D array.

 Collectively, lines 2-24 make a 3D array.


To call values from the array, imagine the 3D array above as a collection of tables. Each nested
bracket cluster is a table with rows and columns. To access or store any element in a 3D array you
need to know its table number, row number, and column number.

An example: You need to access value 25 from the above 3D array. So, first check the table: in this
case, 25 is in table 1 (remember: tables, rows, columns are counted starting at 0, so the second
table is table 1). Once you find the table number now check which row of that table has the value
and then check the column number. So applying above logic, 25 located in table 1, row 1, and
column 1, hence the address is arr[1][1][1]. Print this address and you will get the output: 25.

The Conceptual Syntax of a 3D Array in C


The conceptual syntax for 3D array is this:

data_type array_name[table][row][column];

If you want to store values in any 3D array point first to table number, then row number, and lastly to
column number.

Some hypothetical examples:

arr[0][1][2] = 32;
arr[1][0][1] = 49;

You might also like