/* 9. W.A.P. to read a matrix A(M x N) and find the following using functions :

(a) Sum of elements of each M rows

(b) Sum of elements of each N columns

(c) Sum of all the elements in a matrix */

 

#include<stdio.h>

int sumrow( int , int , int );/* Prototype */                                                                                        int sumcol( int , int , int ); /* Prototype */

void main()
{
    int sumrow(int irow, int ncol, int a[10][10]); /* Function Prototypes */
    int sumcol(int jcol, int mrow, int a[10][10]);
    int i,j,m,n,tot=0,a[10][10]; /* Variable Declarations */
    clrscr();
    printf("Enter how many rows & columns you need for the matrix (M x N) : ");
    scanf("%d %d",&m,&n);
    if ((m!=0) && (n!=0))
    {
        printf("\n\nEnter (M x N) elements of Matrix below :\n");
        for (i=0; i<m; i++)
            for (j=0; j<n; j++)
                scanf("%d",&a[i][j]);
        printf("\n\nSum of the elements are as follows :\n");
        /* Calling function 'sumrow' to add all elements of rows */
        for (i=0; i<m; i++)
        {
            printf("\nSum of elements of Row Number %d is %d",i+1,sumrow(i,n,a));
        }
        printf("\n");
        /* Calling function 'sumcol' to add all elements of the columns */
        for (j=0; j<n; j++)
        {
            printf("\nSum of elements of Column Number %d is %d",j+1,sumcol(j,m,a));
        }
        /* Calling function 'sumrow' to add all elements of the given matrix(M x N) */
        for (i=0; i<m; i++)
            tot+=sumrow(i,n,a);
        printf("\n\nSum of all the elements of the given matrix is %d",tot);
    }
    else printf("!!! Invalid Input Entry. Please Try Again !!!");
    getch();
}
int sumrow(int irow, int ncol, int a[10][10]) /* Function 'sumrow' */
{

    int k,tot=0;
    for (k=0; k<ncol; k++)
    tot+=a[irow][k];
    return(tot);
}
int sumcol(int jcol, int mrow, int a[10][10]) /* Function 'sumcol' */
{
    int k,tot=0;
    for (k=0; k<mrow; k++)
        tot+=a[k][jcol];
    return(tot);
}