it has to appear like this:
#include <iostream>
using namespace std;
int main()
{
float allsales[3] [2] = {{1200.33, 2350.75}, {3677.80, 2456.05}, {750.67, 1345.99}};
float totalsales = 0, ave = 0, sum = 0;
for (int row = 0; row < 3; row )
{
cout << "Ave sale for store "<< row 1 << ": ";
for (int col=0; col < 2; col )
{
totalsales = allsales [row] [col];
sum = 0; // I cant figure out how to code the formula for the sum
ave = sum / 2;
cout << ave <<endl;
}
}
cout << "Total sales : $" << totalsales << endl;
return 0;
}
Also, Im a first year prog student so I cant use some advanced codes/commands etc.,. I can only stick on nested for loops, if else, while
CodePudding user response:
How would you do it by hand?
I would start sum
with an initial value of zero for each row, add to it each element in that row, and then compute the average at the end of the row.
(Don't forget that you can declare variables in other places than first thing in a function.)
And I would also probably first sum the row, and then add the "row sum" to the total.
Like this:
for (int row = 0; row < 3; row )
{
float sum = 0;
for (int col = 0; col < 2; col )
{
sum = allsales[row][col];
}
cout << "Ave sale for store "<< row 1 << ": " << sum / 2 << endl;
totalsales = sum;
}
CodePudding user response:
float allsales[3] [2] = {{1200.33, 2350.75}, {3677.80, 2456.05}, {750.67, 1345.99}};
float totalsales = 0, ave = 0, sum = 0;
for (int row = 0; row < 3; row )
{
cout << "Ave sale for store "<< row 1 << ": ";
sum = 0;
for (int col=0; col < 2; col )
{
totalsales = allsales[row][col];
sum = allsales[row][col];
}
ave = sum / 2;
cout << ave <<endl;
}
cout << "Total sales : $" << totalsales << endl;
return 0;
the only difference between sum and totalsales, is that sum is only for one row at a time, so it needs to be initialized to zero before the start of the second loop. also average needs to be calculated after the inner loop, once calculation of one row of sum is complete.
try dry running the code to understand for loops better