i tried for loop and if statements but just cant figure it out how to do it, if someone can give me some tips and direction. please thanks.
i need to find max, total and average for each region
for (region = 1; region <= 5; region++)
{
cout << setprecision(2) << fixed;
cout << "\nREGION NUMBER " << region << endl;
inputFile >> salesAmount;
if (salesAmount == -1)
cout << "No sales in Region" << region << endl;
If I were you I would create a maximumAmount variable, an numberOfValues variable and a totalAmount variable inside your for-loop.
Then every time you read a value from the file, you increment numberOfValues with 1, add the new value to totalAmount and if the new value is larger than the current maximumAmount you change the maximumAmount to this new value.
When you reach the -1, you calculate the average by deviding the totalAmount by numberOfValues and output the results.
That sounds like you also increment your counter for the -1.
Probably that saved you in region 3, as now you are dividing 0 by 1 (which is legal), if you got the counter right you would try to divide X by 0 (which is illegal so add an if statement to prevent it).
Could you post your updated code using the code tags (the "<>" button)?
for (region = 1; region <= 5; region++)
{
double maximumAmount = 0;
double numofValues = 1; // Here you tell yourself that there is an input value while there is none yet
double totalAmount = 0;
for (numofValues = 1; numofValues <= 4; numofValues++) // Here you set the precision to 2 decimals for four times. In the process you also told yourself that four values have been loaded while there is actually still none.
cout << setprecision(2) << fixed;
cout << "\nREGION NUMBER " << region << endl; // At this moment no values have been loaded yet, so numOfValues should be 0 (but it is 4)
inputFile >> salesAmount;
if (salesAmount == -1)
cout << "No sales in Region" << region << endl;
elsewhile (salesAmount != -1)
{
if (salesAmount < -1)
cout << "*** Invalid Amount: " << salesAmount << endl;
else
cout << setw(22) << "Sales Amount: " << setw(9) << salesAmount << endl;
if (salesAmount > 0)
totalAmount += salesAmount; // Here you calculate the totalAmount, here you should also increment numOfValues (use brackets).
inputFile >> salesAmount;
if (salesAmount > maximumAmount)
maximumAmount = salesAmount;
}
// if (numofValues > 0) // you should add and uncomment this to prevent dividing by 0. Probably you also want to initialize average or add an else statement.
average = totalAmount / numofValues; // Here you get some strange value because you abused numOfvalues and did not count the valid values properly.
You add another variable, say winningRegion of type int in line 11 of your code.
You add another variable, highestAverage of type double in line 15 of your code.
You initialize highestAverage to -1.0 in line 15 of your code.
These two variables are created before and outside the loop, so they will be available inside the loop and outside the loop.
You add an if statement in line 49 of your code, that checks if the average calculated in line 48 of your code is higher than highestAverage. If it is, you set winningRegion to the current region and highestAverage to the current average.
Note that you do this inside the for-loop, so the values that you calculated inside the loop are still available.
After you have completed all your calculations, winningRegion will tell you which region had the highest average and highestAverage will tell you what it was.