i need help finding max, total and average

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

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
ifstream inputFile;

double salesAmount;
int region = 1;

inputFile.open("SalesContest.txt");
if (!inputFile)
{
cout << "Error opening file" << endl;
exit(1);
}

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;

else
while (salesAmount != -1)
{
if (salesAmount < -1)
cout << "*** Invalid Amount: " << salesAmount << endl;
else
cout << setw(22) << "Sales Amount: " << setw(9) << salesAmount << endl;
inputFile >> salesAmount;
}
}
inputFile.close();
return 0;
}



file i used has this
11000.10 3333.33 2000.50 11111.11 -1
4400.44 11100.10 550.50 2200.20 4000.40 -3 -1
-1
5550.50 -2 -3 800.50 35000.25 -1
-2 4555.55 31000.20 2220.25 5000.50 -1


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.
I did that kinda close, still having problems getting the total right. its counting 5 numbers rather than 4 for average.

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)?
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
	ifstream inputFile;

	double salesAmount;
	double average;
	int region = 1;

	inputFile.open("SalesContest.txt");
	if (!inputFile)
	{
		cout << "Error opening file" << endl;
		exit(1);
	}

	for (region = 1; region <= 5; region++)
	{
		double maximumAmount = 0;
		double numofValues = 1;
		double totalAmount = 0;
		
		for (numofValues = 1; numofValues <= 4; numofValues++)
		cout << setprecision(2) << fixed;
		cout << "\nREGION NUMBER " << region << endl;
		inputFile >> salesAmount;
		if (salesAmount == -1)
			cout << "No sales in Region" << region << endl;
		else
			while (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;
				inputFile >> salesAmount;
				if (salesAmount > maximumAmount)
					maximumAmount = salesAmount;
			}
		average = totalAmount / numofValues;

		cout << "Largest Sales Amount " << maximumAmount << endl;
		cout << "region " << region << "total is" << totalAmount << endl;
		cout << "average is " << average << endl;
	}	
	inputFile.close();
	return 0;
}



this is what i got so fare got the total right but average still not good. what should i change??
Last edited on
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
    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;
        else
            while (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.
Last edited on
having problem selecting which region has the highest average
Last edited on
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.
Cant thank you enough bro, due tonight and finally im done. Thanks alot really guided me
Topic archived. No new replies allowed.