Two dimensional Array

A 2D array represents more than one row and column of elements. For example, marks obtained by 3 students in 5 subjects. We can write those marks in 3 rows and 5 columns, as shown here.

50, 60, 70, 80, 90 
55, 65, 75, 85, 95
59, 69, 79, 89, 99

2D arrays can be created in two different ways. The first way is to declare the array and later enter the elements into the array.

double marks[3][5]; 

Here, ‘marks’ is the name of the 2D array. Since there are two pairs of square braces, it is 2D array. The first number 3 represents the maximum number of rows and the next number 5 represents maximum number of columns in each row. So, there is a possibility for storing 3 X 5 = 15 elements into this array.

We can store the elements in this array, using two loops. The outer for loop represents the rows, and the inner for loop represents the columns.

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

In the above code, the marks[i][j] in the scanf() statement represent row position numbers starting from 0 to less than 3. It means the i values will be: 0, 1, 2. j represents column position numbers starting from 0 to less than 5. It means i values will 0, 1, 2, 3, 4,3,4. Thus, the outer loop executes 3 times with i values from 0 to 2 and the inner for loop executes 5 times with j values changing from 0 to 4. So, the elements stored in the marks array can be referenced as:

marks[0][0], marks[0][1], marks[0][2], marks[0][3], marks[0][4]
marks[1][0], marks[1][1], marks[1][2], marks[1][3], marks[1][4]
marks[2][0], marks[2][1], marks[2][2], marks[2][3], marks[2][4]

In general, to refer to any element of a 2D array, we can write marks[i][j]. Here, i represents the row position number, and j represents the column position number. These i and j are called the indexes of the array.

Another way to create a 2D array is to declare the array and initialize it with elements.

double marks[3][5]= {{50,60,70,80,90},

Here,we are declaring marks and storing 3 rows and 5 columns of elements. Each row should be written inside { and } brackets. Then all the rows are inserted inside another { and } brackets. In the preceding declaration, marks array can be written without the number of rows, as: marks[][5]. Row number are optional.

Program to display 2D array in matrix form.

  int i,j;
  /* take 2D array */
  int marks[][5]= {{50,60,70,80,90},
   /* display in matrix form */
   for(i=0; i<3; i++)
      for(j=0; j<5;j++)
         printf("%d\t", marks[i][j]);


50 60 70 80 90
55 65 75 85 95
59 69 79 89 99

A matrix represents several rows and columns of elements. If a matrix has only one row, it is called ‘row matrix’. If a matrix has only one column, then it is called a ‘column matrix’. Similarly, If a matrix has ‘m’ rows and ‘n’ columns, then it is called m X n matirx.

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.