95.3k views
4 votes
Write a program that reads a 5 x 5 two-dimensional array of integers and then prints the row sums and the column sums.C++

User Lucasgcb
by
5.9k points

1 Answer

7 votes

Answer:

Here is code in c++.

#include <bits/stdc++.h>

using namespace std;

int main() {

//declare array of size 5x5

int arr[5][5];

// variables to store row sum and column sum

int row_sum=0,col_sum=0;

cout<<"please enter the elements of the array: "<<endl;

//reading elements of the array

for(int i=0;i<5;i++)

{

for(int j=0;j<5;j++)

{

cin>>arr[i][j];

}

cout<<endl;

}

cout<<"\\sum of each row:"<<endl;

for(int i=0;i<5;i++)

{

row_sum=0;

for(int j=0;j<5;j++)

{

//calculating sum of each row

row_sum=row_sum+arr[i][j];

}

//printing the sum of each row

cout<<"sum of row "<<i+1<<" is :"<<row_sum<<endl;

}

cout<<"sum of each column :"<<endl;

for(int i=0;i<5;i++)

{

col_sum=0;

for(int j=0;j<5;j++)

{

//calculating sum of each column

col_sum=col_sum+arr[j][i];

}

//printing the sum of each column

cout<<"sum of column "<<i+1<<" is :"<<col_sum<<endl;

}

return 0;

}

Step-by-step explanation:

Declare an array of integer type of size 5x5 to store the element from the user. Read the elements from the user.First travers the array row by row and find the sum of each row and print them.After that travers the array column by column and find the sum of each column and print them.

Output:

please enter the elements of the array:

23 43 55 23 78 52 64 88 43 56 23 56 343 76 61 59 34 67 90 98 99 12 3 7 123

sum of each row:

sum of row 1 is :222

sum of row 2 is :303

sum of row 3 is :559

sum of row 4 is :348

sum of row 5 is :244

sum of each column :

sum of column 1 is :256

sum of column 2 is :209

sum of column 3 is :556

sum of column 4 is :239

sum of column 5 is :416

User Daniloxvalle
by
6.3k points