Sum of all element of matrix in C++ by R4R Team

Matrix:
-Multidimensional Array
-Matrix consist of the rows and columns.

Syntax-
array[row][column]
//Matrix with numbers of rows and columns

Example-
Matrix is:
1 2
4 3
Sum of all elements in array is 10

program:

#include < iostream>
using namespace std;
int main()
{
int a[10][10],r,c,i,j,sum=0;
cout<<"Enter rows and columns"<< endl;
cin>>r>>c;
//input in matrix
cout<<"Enter "<< r*c<<" number in matrix"<< endl;
for(i=0;i< r;i++)
{
for(j=0;j< c;j++){
cin>>a[i][j];
}
}
//add all element of array
for(i=0;i< r;i++)
{
for(j=0;j< c;j++)
sum=sum+a[i][j];
}
cout<<"Sum of matrix is: "<< sum;
return 0;
}


output-

Enter rows and column in matrix
2 3
Enter 6 number in matrix
1 2 3 4 5 6
Sum of Matrix is: 21




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!