Advertisement



< Prev
Next >



C - Two Dimensional Array







We will understand a two dimensional(2D)array in three separate parts -


Note:


Like 1D array, 2D Array is also stored on the Heap memory.




Our 2D int array has 3 arrays in it, so let's initialize each of the three arrays with int values.
Note : Number in the second bracket of a 2D array refers to the current index position in a particular array.

/* Creating a 2-D array holding 3 int arrays, with each array holding 3 int values. */

#include<stdio.h>

int main()
{
int twoDA[3][3]; /* 2-D array holding 3 int arrays(each array holding 3 int values) */

twoDA[0][0]=10;  /* inserting 10 in the first array at its 0th index */
twoDA[0][1]=20;  /* inserting 20 in the first array at its 1st index */
twoDA[0][2]=30;  /* inserting 30 in the first array at its 2nd index */

twoDA[1][0]=40;  /* inserting 40 in the second array at its 0th index */ 
twoDA[1][1]=50;  /* inserting 50 in the second array at its 1st index */
twoDA[1][2]=60;  /* inserting 60 in the second array at its 2nd index */

twoDA[2][0]=70;  /* inserting 70 in the third array at its 0th index */
twoDA[2][1]=80;  /* inserting 80 in the third array at its 1st index */
twoDA[2][2]=90;  /* inserting 90 in the third array at its 2nd index */

return 0;
}


Depiction of the effect of the code above in the heap memory(where arrays are stored).

Now let's see how a 2D array is initialized with values-

twoDA[0][0]=10;
The reference to the first array is specified by zero in the first bracket of 2D array. Zero in the second bracket specifies the 0th index in the first array at which we are putting int value 10;

twoDA[0][1]=20;
We are putting an int value(20) in the first array at its 1st index

twoDA[0][2]=30;
We are putting an int value(20) in the first array at its 2nd index

twoDA[1][0]=40;
The reference to the second array is specified by one in the first bracket of 2D array. Zero in the second bracket specifies the 0th index of the second array at which we are putting int value 40;

twoDA[1][1]=50;
We are putting an int value(50) in the second array at its 1st index

twoDA[1][2]=60;
We are putting an int value(60) in the second array at its 2nd index




Reading a 2-D array


We can read any element of an array contained in a 2D array using its index position in the array. For example, to read and display an element at 0th index of the 1st array in our 2D array, is simply -

printf(twoDA[1][0]);	/* Reading the 1st array and its 0th index */

To read all the elements of a 2D array with ease, we will use 2 for-loops, one inside another i.e. nested -

/* Creating a 2-D array holding 3 int arrays, with each array holding 3 int values. */

#include<stdio.h>

int main()
{
int twoDA[3][3]; /* 2-D array holding 3 int arrays(each array holding 3 int values) */

twoDA[0][0]=10;  /* inserting 10 in the first array at its 0th index */
twoDA[0][1]=20;  /* inserting 20 in the first array at its 1st index */
twoDA[0][2]=30;  /* inserting 30 in the first array at its 2nd index */

twoDA[1][0]=40;  /* inserting 40 in the second array at its 0th index */ 
twoDA[1][1]=50;  /* inserting 50 in the second array at its 1st index */
twoDA[1][2]=60;  /* inserting 60 in the second array at its 2nd index */

twoDA[2][0]=70;  /* inserting 70 in the third array at its 0th index */
twoDA[2][1]=80;  /* inserting 80 in the third array at its 1st index */
twoDA[2][2]=90;  /* inserting 90 in the third array at its 2nd index */



/* Reading an array */
for(int i=0; i<3; i++)
{
	for(int j=0; j<3;j++)
	{
		printf("%d ", twoDA[i][j]);
	}

}

return 0;
}

Output-

10
20
30
40
50
60
10
20
10





Please share this article -




< Prev
Next >
< Array in C
Functions in C >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement

Notifications



Please check our latest addition

PYTHON and DJANGO


Advertisement