Loop Control Statements in C Introduction to HTML How to use the Github API The image tag, anchor tag and the button tag Ordered and Unordered Lists in HTML The division tag HTML Forms Tables in HTML Introduction to C Programming Introduction to Python Varibles and Datatypes in Python Operators in Python Typecasting in Python Input and Output in Python If Else in Python Loops in Python Break, Continue and Pass in Python Python practice section 1 Lists in Python Tuple in Python

2-D Arrays in C

Like 1-D array is a collection of data items, a 2-D array is a collection of 1-D arrays. Suppose you want to store the marks of different students in one subject, you can use a 1-D array to do it. But suppose there are 3 different subjects, then you would declare three 1-D arrays to store all the marks of 3 subjects. Instead, just declare one 2-D array and your job will be done. In 2-D arrays, elements are stored in the form of rows and columns. Every subject represents one row and each row contains the marks of different students in that subject. Let's see how we can do this.

Declaration of 2D array

Here we need to specify two things. First, the number of 1-D array it contains i.e the number of rows (say m) and secondly the number of data items present in each 1-D array i.e. the number of columns (say n).
To declare an integer array of size m x n, we write -

    int a[m][n];
                
Similarily, for float and other data types.

Initialization of 2D array

Just like a 1D array, we can specify the values in curly brackets as -

    int a[3][3]={1,2,3,4,5,6,7,8,9};
        
Or, we can also write -

    int a[3][3]={ 
                  {1,2,3},
                  {4,5,6},
                  {7,8,9} 
                };
        
We have three 1-D arrays containing three items each.

Accessing values in 2D array

Suppose we want to get the value 4 which is stored in the second row and first column. Now, if we write a[1], it refers to the second row and not any value. To get the first value from the second row, we again need to index. Hence, we write a[1][0] to get the desired value.

    #include <stdio.h>
    void main()
    {

        int a[3][3]={ 
            {1,2,3},
            {4,5,6},
            {7,8,9} 
          };

    printf("1st element of 2nd row = %d \n",a[1][0]);
    printf("3rd element of 1st row = %d \n",a[0][2]);
    printf("2nd element of 3rd row = %d \n",a[2][1]);

    }
        

Output

        1st element of 2nd row = 4 
        3rd element of 1st row = 3 
        2nd element of 3rd row = 8  
    

Taking 2D array as input from the user

The first element of the first row would be stored at the 0th row and 0th column. So, we can write scanf("%d",&arr[0][0]) to store it. The second element of the first row would be stored at 0th row and 1st column.Similarly, the first element of the second row would be stored at 1st row and 2nd column and so on.
In code, we can do it like this -

    #include <stdio.h>
    void main()
    {   
        int m,n;
        printf("Enter the number of rows : ");
        scanf("%d",&m);
        printf("Enter the number of columns : ");
        scanf("%d",&n);
        int arr[m][n];
        printf("Enter the elements of the array : ");
        for(int i = 0 ; i < m ; i++)
        {
            for(int j = 0 ; j < n ; j++)
            scanf("%d",&arr[i][j]);

            printf("\n");
        }
        
        // Printing all the elements of array

        printf("The elements of the array are : \n");
        for(int i = 0 ; i < m ; i++)
        {   
            printf("Row %d : ",i);

            for(int j = 0 ; j < n ; j++)
            printf("%d ",arr[i][j]);

            printf("\n");
        }
    }
      

Output

    Enter the number of rows : 3
    Enter the number of columns : 3
    Enter the elements of the array : 4 7 5 6 8 10 14 16 18
    The elements of the array are : 
    Row 1 : 4 7 5 
    Row 2 : 6 8 10 
    Row 3 : 14 16 18