Two Dimensional Arrays in C

Example program 3

Here is a program that consists of multiple sub-programs regarding two-dimensional arrays. A lot can be learnt from analyzing this one program. This program

  1. Inputs the elements for 2 matrices.

  2. Adds both of them and prints the result.

  3. Finds the product of them and prints it.

  4. Finds the transpose of the 1st matrix entered.

  5. Finds the sum of the row and column of the 1st matrix.

To scan and print the two-dimensional array

for(i=0;i<r;i++)
    {
        for(j=0;j<c;j++){
            scanf("%d",&ar[i][j]);

        }
        printf("\n");
    }
    for(i=0;i<r;i++)
    {
        for(j=0;j<c;j++){

            printf("%d\t",ar[i][j]);
        }
        printf("\n");
    }

The main program is

#include<stdio.h>
#include<math.h>
int main(){
    printf("Program to input 2d array and find the sum, product, transpose, and the sum of the row and colunms\n");
    int r,c,i,j;
    printf("Enter the number of rows and columns in the array\n");
    scanf("%d",&r);
    scanf("%d",&c);
    printf("Enter the elements in the elements");
    int ar[r][c],arr[r][c],sum[r][c],pro[r][c];
    for(i=0;i<r;i++)
    {
        for(j=0;j<c;j++){
            scanf("%d",&ar[i][j]);

        }
        printf("\n");
    }
    for(i=0;i<r;i++)
    {
        for(j=0;j<c;j++){

            printf("%d\t",ar[i][j]);
        }
        printf("\n");
    }
    printf("Enter the elements of the 2nd matrix\n");
    for(i=0;i<r;i++)
    {
        for(j=0;j<c;j++){
            scanf("%d",&arr[i][j]);
        }
        printf("\n");
    }
    for(i=0;i<r;i++)
    {
        for(j=0;j<c;j++){
            printf("%d\t",arr[i][j]);
        }
        printf("\n");
    }


//    1st part to input the value



    printf("Sum:\n");
    for(i=0;i<r;i++)
    {
        for(j=0;j<c;j++){
            sum[i][j]=ar[i][j]+arr[i][j];
        }
    }
    for(i=0;i<r;i++)
    {
        for(j=0;j<c;j++){
            printf("%d\t",sum[i][j]);
        }
        printf("\n");
    }


//    Finds the sum


    printf("Product\n");
    for(i=0;i<r;i++)
    {
        for(j=0;j<c;j++){
            pro[i][j]=ar[i][j]*arr[i][j];
        }
    }
    for(i=0;i<r;i++)
    {
        for(j=0;j<c;j++){
            printf("%d\t",pro[i][j]);
        }
        printf("\n");
    }


//    Finds the product



    printf("Transpose of sum\n");
    for(i=0;i<r;i++)
    {
        for(j=0;j<c;j++){
            printf("%d\t",sum[j][i]);
        }
        printf("\n");
    }



//    Finds the transpose 



    printf("Sum of rows\n");
    for(i=0;i<r;i++)   
    {
        int rsum=0;
        for(int j=0;j<c;j++)
        {
        rsum=rsum+ar[i][j];
        }
        printf("\nSum of all the elements in row %d is %d\n",i,rsum);
    }

    printf("Sum of columns\n");
    for(i=0;i<r;i++)
    {
        int csum=0;
        for(int j=0;j<c;j++)
        {
        csum=csum+ar[j][i];
        }
        printf("\nSum of all the elements in column %d is %d\n",i,csum);
    }


//    Finds the sum of the rows and columns 


    return 0;
}

Thanks for reading this blog hope this helped understand two-dimensional arrays. Don't forget to follow.

Enjoy learning...