1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
#include <iostream>
using namespace std;
int main()
{
int total, average;
int sales[][4] = {
{ 59, 32, 7, 6 },
{ 44, 16, 17, 33 },
{ 21, 6, 9, 56 }
};
for (int row = 0; row < 3; row++) {
// we are at the start of a row, reset the
// total variable ready.
total = 0;
// loop through the values for this row adding
// each one to the total variable.
for (int column = 0; column < 4; column++) {
// add value to total.
total += sales[row][column];
// display the value (if you need it)
cout << sales[row][column] << " ";
}
// total holds row total, and we had 4 items so
// calculate the average.
average = total / 4;
// display totals.
cout << " - Total: " << total << ", Average: " << average << endl;
}
return 0;
}
|
59 32 7 6 - Total: 104, Average: 26
44 16 17 33 - Total: 110, Average: 27
21 6 9 56 - Total: 92, Average: 23
|