Include
Include
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++
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,
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.
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.
arr[0][1][2] = 32;
arr[1][0][1] = 49;