I need help adding a sum function to my c++ program, if possible in a loop. The
ID: 3537597 • Letter: I
Question
I need help adding a sum function to my c++ program, if possible in a loop. The sum function should be the sum of the array rows that is placed at the end of each row. I have down most of the problem as shown below with the original instructions also. The example of what the output is shown with the instructions.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int size[4][5] = {{1, 20, 3, 41, 5},
{5, 65, 22, 14, 23},
{21, 33, 10, 68, 43},
{88 , 22, 1, 83, 27}};
int total = 0;
for(int row=0; row<4; row++) {
for (int column=0; column<5; column++) {
cout << size[row][column] << " ";
}
cout << endl;
}
}
Write a program that will
Declare and initialize a two dimensional array (4x5)
Output the array in row column form. Also calculate and print the sum of each
row.
Example output
1 20 3 41 5 sum
5 65 22 14 23 sum
21 33 10 68 43 sum
88 22 1 83 27 sum
Explanation / Answer
int a[4][5] = {{1,20,3,41,5},{5, 65, 22, 14, 23},{21, 33, 10, 68, 43},{88, 22, 1, 83, 27}};
for(int i=0;i<4;i++){
for(int j=0;j<5;j++){
cout<<a[i][j]<<" ";
}
int t=a[i][0]+a[i][1]+a[i][2]+a[i][3]+a[i][4];
cout<<t<<" ";
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.