TechoSagar: Sum of elements in a matrix

Search

Google Alert - jobs

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
Your Code
#include<iostream>

using namespace std;

int main()
{
int t;
cin>>t;
while(t--)
{
int mat[10][10];
int m,n;
cin>>n>>m;
int i,j;
int sum=0;
for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
{
cin>>mat[i][j];
sum=sum+mat[i][j];
}
}
cout<<sum<<endl;
}
}

Follow us

Follow Bijendra Kumar on Facebook