Sum of Each Row and Column of a Matrix in C++ language
ADVERTISEMENTS
Sum of each row and column of a matrix in c++ language. In this article, you will learn how to find the sum of each row and column of a matrix in c++ language.
Source Code
// Sum of Each Row and Column of a Matrix in C++ language
#include <iostream>
using namespace std;
int main() {
int a[10][10], i, j, x, y, sum = 0;
cout << "-----Enter the number of rows & columns of the matrix-----\n";
cin >> x >> y;
cout << "\n-----Enter the co-efficients of the matrix-----\n";
for (i = 0; i < x; ++i) {
for (j = 0; j < y; ++j) {
cin >> a[i][j];
}
cout << "\n";
}
for (i = 0; i < x; ++i) {
for (j = 0; j < y; ++j) {
sum = sum + a[i][j] ;
}
cout << "The Sum of the " << i << " position row is = " << sum << "\n";
sum = 0;
}
cout << "\n";
sum = 0;
for (j = 0; j < y; ++j) {
for (i = 0; i < x; ++i) {
sum = sum + a[i][j];
}
cout << "The Sum of the " << j << " position column is = " << sum << "\n";
sum = 0;
}
return 0;
}
Output
-----Enter the number of rows & columns of the matrix-----
3
4
-----Enter the co-efficients of the matrix-----
23
45
23
54
24
34
65
76
34
86
56
87
The Sum of the 0 position row is = 145
The Sum of the 1 position row is = 199
The Sum of the 2 position row is = 263
The Sum of the 0 position column is = 81
The Sum of the 1 position column is = 165
The Sum of the 2 position column is = 144
The Sum of the 3 position column is = 217