Tutorial - Multidimensional Arrays - Part 1


Submit solution

Points: 10 (partial)
Time limit: 2.0s
Memory limit: 634M

Author:
Problem type
Allowed languages
C

Tutorial

Compiled from multiple sources on the internet.

Since this is a tutorial, there is no submission data for this. Please use the IDE for practice.

Tutorial - Multidimensional Arrays

An array of arrays is known as 2D array. The two dimensional (2D) array in C programming is also known as matrix. A matrix can be represented as a table of rows and columns. 

Two dimensional(2D) Array Example

This program demonstrates how to store the elements entered by user in a 2d array and how to display the elements of a two dimensional array.

#include<stdio.h> 
int main(){ /* 2D array declaration*/     
int disp[2][3]; /*Counter variables for the loop*/     
int i, j; 
//Reading array elements    
for(i=0; i<2; i++) {     
           for(j=0;j<3;j++) {     
               scanf("%d", &disp[i][j]);    
            }   
 }     
//Displaying array elements    
 printf("Two Dimensional array elements:\n");     
for(i=0; i<2; i++) {     
         for(j=0;j<3;j++) {     
              printf("%d ", disp[i][j]);     
              if(j==2){ printf("\n"); }

         }    
 } 

return 0;

}

Input:

1 2 3 4 5 6

Output:

Two Dimensional array elements:

1 2 3

4 5 6

Important Note:

1. Initialization of the Array

There are two ways to initialize a two Dimensional arrays during declaration.

int disp[2][4] = {    
    {10, 11, 12, 13},    
    {14, 15, 16, 17}    
};

OR

int disp[2][4] = { 10, 11, 12, 13, 14, 15, 16, 17};

Although both the above declarations are valid, It is recommended to use the first method as it is more readable, because it is easier to visualize the rows and columns of 2d array in this method.

2. Always specify columns

We already know, when we initialize a normal array (or you can say one dimensional array) during declaration, we need not to specify the size of it. However that’s not the case with 2D array, you must always specify the second dimension even if you are specifying elements during the declaration.

Example

/* Valid declaration*/
int abc[2][2] = {1, 2, 3 ,4 }  

/* Valid declaration*/
int abc[][2] = {1, 2, 3 ,4 }  

/* Invalid declaration – you must specify second dimension*/
int abc[][] = {1, 2, 3 ,4 }   

/* Invalid because of the same reason  mentioned above*/
int abc[2][] = {1, 2, 3 ,4 }

3. Getting User input

We can calculate how many elements a two dimensional array can have by using this formula: The array arr[n1][n2] can have n1n2 elements. The array that we have in the example below is having the dimensions 5 and 4. These dimensions are known as subscripts. So this array has first subscript value as 5 and second subscript value as 4. So the array abc[5][4] can have 54 = 20 elements.

To store the elements entered by user we are using two for loops, one of them is a nested loop. The outer loop runs from 0 to the (first subscript -1) and the inner for loops runs from 0 to the (second subscript -1). This way the the order in which user enters the elements would be abc[0][0], abc0, abc0…so on.

#include<stdio.h>
int main(){
   /* 2D array declaration*/
   int abc[5][4];
   /*Counter variables for the loop*/
   int i, j;
   for(i=0; i<5; i++) {
      for(j=0;j<4;j++) {
         printf("Enter value for abc[%d][%d]:", i, j);
         scanf("%d", &abc[i][j]);
      }
   }
   return 0;
}

In above example, I have a 2D array abc of integer type. Conceptually you can visualize the above array like this:

2D Array Visualization

However the actual representation of this array in memory would be something like this:

Actual M/y Representation of 2D Array

More on this in the next tutorial.


Comments

There are no comments at the moment.