Write code for the function printRowSums. It should print the sum of each row of the matrix (put a single space after each sum).
For the matrix shown here:
You should print "15 40 "
Hints:
- Going to want nested loops...
Code:
#include <iostream>
#include <climits>
using namespace std;
const int NUM_COLS = 5;
void printRowSums(const int values[][NUM_COLS], int numRows) {
//Do not modify anything on or above the line below this
//YOUR_CODE_BELOW
//YOUR_CODE
//YOUR_CODE_ABOVE
//Do not modify anything on or below the line above this
}
int main()
{
int nums[2][NUM_COLS] = {{1, 2, 3, 4, 5},
{11, 2, 3, 4, 5}};
int nums2[5][NUM_COLS] = {{21, 2, 3, 4, 5},
{21, 2, 3, 4, 5},
{1, 12, 3, 4, 5},
{2, 2, 23, 4, 5},
{1, 2, 3, 24, 5}};
printRowSums(nums, 2);
cout << endl;
printRowSums(nums2, 5);
cout << endl;
return 0;
}
Tried code:
#include <iostream>
#include <climits>
using namespace std;
const int NUM_COLS = 5;
void printRowSums(const int values[][NUM_COLS], int numRows) {
//Do not modify anything on or above the line below this
//YOUR_CODE_BELOW
//YOUR_CODE
long rowTotal=0;
int i,j;
for (i=0; i<numRows; i++)
{
rowTotal = 0;
for (j=0; j<NUM_COLS; j++)
{
rowTotal += values[i][j];
}
cout << (i+1) << rowTotal << endl;
}
//YOUR_CODE_ABOVE
//Do not modify anything on or below the line above this
}
int main()
{
int nums[2][NUM_COLS] = {{1, 2, 3, 4, 5},
{11, 2, 3, 4, 5}};
int nums2[5][NUM_COLS] = {{21, 2, 3, 4, 5},
{21, 2, 3, 4, 5},
{1, 12, 3, 4, 5},
{2, 2, 23, 4, 5},
{1, 2, 3, 24, 5}};
printRowSums(nums, 2);
cout << endl;
printRowSums(nums2, 5);
cout << endl;
return 0;
}
- Going to want nested loops...
Results:
Input:
OutputExpected |
 |
|
115
225
135
235
325
436
535
Expected:
Kat H.
Can you please edit it so that 15 25 are on one line and 35 35 25 36 35 are on the next line? Thank you.05/17/21