Monday, 21 August 2017

Sum of elements in a matrix

Given a non null integer matrix,calculate the sum of its elements.
Input: First line contains T , the number of test cases.First line of each test contains 2 integers N,M and N lines follow which contain M spaced integers.

Output:Single line for each test case containing the sum

Constraints: 1<= N,M<=10 , elements of matrix   -1000<=matrix<=1000

Example:
Input:
1

2 3 
1 0 0
8 -9 -1
Output 
-1
                                           C-Solution
#include <stdio.h>

int main() {
    int t,m,n,sum=0,j;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d ",&m);scanf("%d",&n);int a[m][n];
        while((--m)>=0)
        {
            for(j=0;j<n;j++)
            {
                scanf("%d",&a[m][j]);sum+=a[m][j];
            }
        }
        printf("%d\n",sum);
        sum=0;
    }
     return 0;
}

No comments:

Post a Comment