write a c++ programe which calculates sum of each column and row separately .for this, first create a 2D array 3 rows , 5 columns and then ask the user to input values in the array . after that , first calculate sum of each row separately and display that sum. then calculate sum of each column separatel and display that sum on the screen. to achieve such output , ou need to follow the following instructions?
#include <iostream>
using namespace std;
const int M=3;
const int N=5;
int main() {
double arr[M][N];
for (int i=0; i<M; i++) {
cout << "Enter " << N << " values for " << i+1 << "-th row: ";
for (int j=0; j<N; j++) {
cin >> arr[i][j];
}
}
double sum;
for (int i=0; i<M; i++) {
sum = 0.0;
for (int j=0; j<N; j++) {
sum += arr[i][j];
}
cout << "The sum of " << i+1 << "-th row is " << sum << endl;
}
for (int j=0; j<N; j++) {
sum = 0.0;
for (int i=0; i<M; i++) {
sum += arr[i][j];
}
cout << "The sum of "<< j+1 << "-th column is " << sum << endl;
}
return 0;
}
Comments
Good
Leave a comment