Answer to Question #325524 in C++ for naqibullah

Question #325524

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?


1
Expert's answer
2022-04-07T17:37:56-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Naqib Ullah
08.04.22, 02:47

Good

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS