Mastering Three Dimensional Arrays: A Comprehensive Guide

Three dimensional Array is a combination of several 2D array. For example, marks obtained by a group of students in a class become a 2D array. Now, the marks obtained by several such classes in a school become an example for 3D array. Three dimensional Array can be created in 2 ways. The first way is to declare the array and later enter the elements into the array.

int marks[3][3][5];

Here, ‘marks’ is the name of the Three dimensional Array. Since there are three pairs of square braces, it is a 3D array. The first number 3 represents that this array contains 3-2D arrays. The next numbers, 3 and 5 represent the row and column in each 2D array. So, there is a possibility for storing 3 X 3 X 5=45 elements into this array.

We can store the elements into this array, using three for loops. The outer for loop represents the number of 2D arrays in this 3D array, the inner for loop represents the rows in 2D array and the inner most for loop represents the columns in 2D array.

for(i=0; i<3; i++)
  for(j=0; j<3; j++)
     for(k=0; k<5; k++)
       {
          printf("\n Enter element:");
          scanf("%d",&marks[i][j][k]);
       }

The second way of creating array is to declare the array and initialize it directly.

int marks[3][3][5] = {
   {{11, 12, 13, 14, 15},
    {16, 17, 18, 19, 20},
    {21, 22, 23, 23, 25}},

    {{26, 27, 28, 29, 30},
     {31, 32, 33, 34, 35},
     {36, 37, 38, 39, 40}},

    {{41, 42, 43, 44, 45},
     {46, 47, 48, 49, 50},
     {51, 52, 53, 54, 55}},
               };

There are three 2D arryas. Each 2d Array is enclosed inside a pair of { and }. And each row is enclosed inside a pair of { and }. The complete 3D array is again enclosed inside another { and }. we can omit the first index and declare as:

 int marks[][3][5]=...

Program to initialize and display the elements of 3D array. – Three dimensional Array

#include<stdio.h>
void main()
{
  int i,j,k;
  /* declare 3D array with three 2D arrays*/
  int marks[3][3][5]= {
              {{11, 12, 13, 14, 15},
               {16, 17, 18, 19, 20},
               {21, 22, 23, 23, 25}},

              {{26, 27, 28, 29, 30},
               {31, 32, 33, 34, 35},
               {36, 37, 38, 39, 40}},

              {{41, 42, 43, 44, 45},
               {46, 47, 48, 49, 50},
               {51, 52, 53, 54, 55}},
               }
   /* read and display the 3D array elements. */
     for(i=0; i<3; i++)
     {
          for(j=0; j<3;j++)
          {
              for(k=0; k<5; k++)
                  printf("%d\t", marks[i][j][k]);
              printf("\n");
          }
          printf("\n");
     }
}

Output:

11  12  13  14  15
16  17  18  19  20
21  22  23  24  25

26  27  28  29  30
31  32  33  34  35
36  37  38  39  40

41  42  43  44  45
46  47  48  49  50
51  52  53  54  55
Naveed Tawargeri
 

Hi, I'm Naveed Tawargeri, and I'm the owner and creator of this blog. I'm a Software Developer with a passion for Programming.