Perform various operations on elements of the matrix

C program to to read a matrix and find the following using functions

  • Sum of the elements of each row
  • Sum of the elements of each column
  • Sum of main diagonal elements
  • Sum of minor diagonal elements
  • sum of all the elements of the matrix
  • product of all the elements of the matrix
  • Program

    #include<stdio.h>
    #include<conio.h>
    #define MAX 10
    
    void read_matrix(int mat[MAX][MAX], int row, int col)
    {
        int i, j;
        for(i=0;i<row;i++)
        {
            for(j=0;j<col;j++)
            {
                printf("Element [%d][%d]\t:",i,j);
                scanf("%d",&mat[i][j]);
            }
        }
    }
    
    void print_matrix(int mat[MAX][MAX], int row, int col)
    {
        int i, j;
        for(i=0;i<row;i++)
        {
            for(j=0;j<col;j++)
            {
                printf("%d\t",mat[i][j]);
            }
            printf("\n");
        }
    }
    
    void sum_row(int mat[MAX][MAX], int row, int col)
    {
        int i, j, sum;
        printf("\n");
        for(i=0;i<row;i++)
        {
            sum = 0;
            for(j=0;j<col;j++)
            {
                sum = sum + mat[i][j];
            }
            printf("Sum of row %d\t:%d",i+1,sum);
            printf("\n");
        }
    }
    
    void sum_col(int mat[MAX][MAX], int row, int col)
    {
        int i, j, sum;
        printf("\n");
        for(i=0;i<col;i++)
        {
            sum = 0;
            for(j=0;j<row;j++)
            {
                sum = sum + mat[j][i];
            }
            printf("Sum of column %d\t:%d",i+1,sum);
            printf("\n");
        }
    }
    
    void sum_diag(int mat[MAX][MAX], int row, int col)
    {
        int i, j, sum, sum1;
        printf("\n");
    
        if(row == col)
        {
            sum = 0;
            sum1 = 0;
            for(i=0;i<row;i++)
            {
                for(j=0;j<col;j++)
                {
                    if(i==j)
                    {
                        sum = sum + mat[i][j];
                    }
                    if(row-i-1 == j)
                    {
                        sum1 = sum1 + mat[i][j];
                    }
                }
            }
            printf("Sum of main diagonal\t:%d",sum);
            printf("\nSum of minor diagonal\t:%d",sum1);
        }
        else
        {
            printf("For diagonals matrix should be a square matrix");
        }
    }
    
    void sum_prod_ele(int mat[MAX][MAX], int row, int col)
    {
        int i, j, sum;
        long int prod;
        printf("\n");
        sum = 0;
        prod = 1;
        for(i=0;i<row;i++)
        {
            for(j=0;j<col;j++)
            {
                sum = sum + mat[i][j];
                prod = prod * mat[i][j];
            }
        }
        printf("Sum of elements of the matrix\t:%d",sum);
        printf("\nProduct of elements of the matrix\t:%ld",prod);
    }
    
    void main()
    {
        int mat[MAX][MAX];
        int row, col;
    
        printf("Enter the number of rows\t:");
        scanf("%d",&row);
        printf("Enter the number of columns\t:");
        scanf("%d",&col);
    
        printf("Enter the elements of the matrix\n");
        read_matrix(mat, row, col);
    
        printf("\nMatrix entered is\n");
        print_matrix(mat, row, col);
    
        sum_row(mat, row, col);
        sum_col(mat, row, col);
        sum_diag(mat, row, col);
        sum_prod_ele(mat, row, col);
    
        getch();
    }
    

    Output

    Enter the number of rows        :3
    Enter the number of columns     :3
    Enter the elements of the matrix
    Element [0][0]  :1
    Element [0][1]  :2
    Element [0][2]  :3
    Element [1][0]  :4
    Element [1][1]  :3
    Element [1][2]  :2
    Element [2][0]  :1
    Element [2][1]  :2
    Element [2][2]  :3
    
    Matrix entered is
    1       2       3
    4       3       2
    1       2       3
    
    Sum of row 1    :6
    Sum of row 2    :9
    Sum of row 3    :6
    
    Sum of column 1 :6
    Sum of column 2 :7
    Sum of column 3 :8
    
    Sum of main diagonal    :7
    Sum of minor diagonal   :7
    Sum of elements of the matrix   :21
    Product of elements of the matrix       :864