Multidimensional Array in C

Multidimensional Array  in C Language:

 On this page we will discuss about what is multidimensional array in C language ( two dimensional and three dimensional arrays) with some examples.To understand what is multidimensional array we need to know about array first.An array is  a data structure containing a number of  data value( all of which are of same data type).So, multidimensional arrays can be defined as the array of arrays.

Multidimensional Array in C

Multidimensional Array in C Programming

You can think of multidimensional arrays as the combination of multiple arrays.Multidimensional array can be defined as the array of arrays they store data in tabular form which is of homogeneous type.

The general syntax of declaring N-dimensional arrays is: 
data_type array_name[size1][size2]....[sizeN];

Here, Data_type is the type of you data you want to store like int,char,float,etc. array_name is the name of array which follows the rules of identifier and size1 ,size2 …… is the dimension of array.

Numbers of elements this array can store is (size1 x size2 x ……… x sizeN )

Two-dimensional arrays

Two-dimensional is one of the most basic and simplest form of multidimensional array.

syntax:

data_type array_name[x][y];

Initialization of two-dimensional array:

int x[2][3] = {0, 1 ,2 ,3 ,4 , 5 }     //first method

int x[2][3] = {{0,1,2}, {3,4,5}};  // second method

int x[3][4];			//third method
for (int i = 0; i<3; i++)
  {
    for (int j = 0; j<4; j++)
      {
	cin >> x[i][j];
      }
  }
Multidimensional Array in C 2d array

Example : Two-dimensional array to store and print values

Run
#include <stdio.h>

int main ()
{
  int a[4][3] = { {0, 0, 0}, {1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
  int i, j;

  /* output each array element's value */
  for (i = 0; i < 4; i++)
    {

      for (j = 0; j < 3; j++)
	{
	  printf ("a[%d][%d] = %d\n", i, j, a[i][j]);
	}
    }

  return 0;
}

Output:

a[0][0] = 0
a[0][1] = 0
a[0][2] = 0
a[1][0] = 1
a[1][1] = 2
a[1][2] = 3
a[2][0] = 4
a[2][1] = 5
a[2][2] = 6
a[3][0] = 7
a[3][1] = 8
a[3][2] = 9

Three-dimensional array

Three deminsional array is same as the two-dimensional array.in this the number of dimensions increases.

syntax:

data_type array_name[x][y][z];

Initialization of three-dimensional array:

int x[2][2][3] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,        //first method
                 11};    

int x[2][2][3] =                                         // second method
 { 
   { {0,1,2}, {3,4,5}},
   { {6,7,8}, {9,10,11}}
 }; 

int x[2][2][3];			//third method
for (int i = 0; i < 2; ++i)
{
for (int j = 0; j < 2; ++j)
{
for (int k = 0; k < 3; ++k)
{
cin >>[i][j][k];
}
}
}

Example : Three-dimensional array to store and print values

Run
#include <stdio.h>

int main ()
{

  int p[2][2][3] = { {{0, 1, 2}, {3, 4, 5}},
  {{6, 7, 8}, {9, 10, 11}} };

  for (int i = 0; i < 2; ++i)
    {
      for (int j = 0; j < 2; ++j)
	{
	  for (int k = 0; k < 3; ++k)
	    {
	      printf ("Element at p[%i][%i][%i] = %d\n", i, j, k, p[i][j][k]);

	    }
	}
    }
  return (0);
  }

Output:

Element at p[0][0][0] = 0
Element at p[0][0][1] = 1
Element at p[0][0][2] = 2
Element at p[0][1][0] = 3
Element at p[0][1][1] = 4
Element at p[0][1][2] = 5
Element at p[1][0][0] = 6
Element at p[1][0][1] = 7
Element at p[1][0][2] = 8
Element at p[1][1][0] = 9
Element at p[1][1][1] = 10
Element at p[1][1][2] = 11

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription