Two-Dimensional Arrays in C Programming


The two-dimensional array can be defined as an array of arrays. The 2D array is organized as matrices which can be represented as the collection of rows and columns. However, 2D arrays are created to implement a relational database lookalike data structure. It provides ease of holding the bulk of data at once which can be passed to any number of functions wherever required.
Syntax :
     datatype array_name [ Rows ] [ Columns ] ;
Example :
     int arr [ 100 ] [ 100 ] ;

This program is a simple C program that performs matrix addition. The program first declares three 2-dimensional arrays, "a," "b," and "c," with a maximum size of 100 x 100. The program then prompts the user to enter the number of rows and columns for the matrices. Next, the program uses nested for loops to prompt the user to enter the values for the "a" and "b" matrices. Finally, the program performs matrix addition by adding the corresponding elements of the "a" and "b" matrices and storing the result in the "c" matrix. The program then prints out the resulting matrix, "c."

Source Code

//2D Arrays
#include<stdio.h>
int main()
{
    int a[100][100],b[100][100],c[100][100];
    int i,j,n,m;
    printf("\nEnter The Rows : ");
    scanf("%d",&n);
    printf("\nEnter The Columns : ");
    scanf("%d",&m);
 
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            printf("Enter The Value of a[%d][%d] : ",i,j);
            scanf("%d",&a[i][j]);
        }
    }
 
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            printf("Enter The Value of b[%d][%d] : ",i,j);
            scanf("%d",&b[i][j]);
        }
    }
 
     for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            c[i][j]=a[i][j]+b[i][j];
            printf("\t%d",c[i][j]);
        }
        printf("\n");
    }
    return 0;
}
 
To download raw file Click Here

Output

Enter The Rows : 3

Enter The Columns : 2
Enter The Value of a[0][0] : 21
Enter The Value of a[0][1] : 22
Enter The Value of a[1][0] : 23
Enter The Value of a[1][1] : 24
Enter The Value of a[2][0] : 25
Enter The Value of a[2][1] : 26
Enter The Value of b[0][0] : 27
Enter The Value of b[0][1] : 28
Enter The Value of b[1][0] : 29
Enter The Value of b[1][1] : 30
Enter The Value of b[2][0] : 31
Enter The Value of b[2][1] : 32

        48      50
        52      54
        56      58

List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C