codescracker


c

C Three Dimensional Array Program



« Previous Program Next Program »


Three Dimensional Array in C

Three dimensional (3D) array contains three for loops in programming. So to initialize and print three dimensional array in C programming, you have to use three for loops as shown in the following program.

C programming Code for Three Dimensional (3D) Array

A three dimensional (3d) array can be thought of as an array of arrays of arrays. Following is a simple C program to initialize three dimensional (3D) array of dimension 3*4*2, then it will access some elements present in the array and display the element on the screen :

/* C Program - Three Dimensional Array */
		
#include<stdio.h>
#include<conio.h>
void main()
{
	clrscr();
	int arr[3][4][2] = {
			      {
				 {2, 4},
				 {7, 8},
				 {3, 4},
				 {5, 6}
			      },
			      {
				 {7, 6},
				 {3, 4},
				 {5, 3},
				 {2, 3}
			      },
			      {
				 {8, 9},
				 {7, 2},
				 {3, 4},
				 {5, 1}
			      }
			   };
	printf("arr[0][0][0] = %d\n",arr[0][0][0]);
	printf("arr[0][2][1] = %d\n",arr[0][2][1]);
	printf("arr[2][3][1] = %d\n",arr[2][3][1]);
	getch();
}

When the above c program is compile and executed, it will produce the following result:

three dimensional array programming in c

Here the outer array has three elements, each of which is a 2-D array of four 1-D arrays, each of which contains two integers. It means, a 1-D array of two elements is constructed first. Then four such 1-D arrays are placed one below the other to give a 2-D array containing four rows. Then, three such 2-D arrays are placed one behind the other to yield a 3-D array containing three 2-D arrays

Same Program in Other Programming Language

You may also like same program in other programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test