cpp PrimerPlus PE 6-2

I know that there is a couple of different ways I could have gone with this, but it works. Reason why I had to use the setprecision was because it kept coming up with E values. Just still learning this...

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
  /*
*Write a program that reads up to 10 donation values into an array of double. (Or, if
you prefer, use an array template object.) The program should terminate input on
non-numeric input. It should report the average of the numbers and also report
how many numbers in the array are larger than the average.
*
*
*/
#include <iostream>
#include <iomanip>
#include <array>
using namespace std;
int main()
{
    const int arSize = 10;
    double avg = 0;
    double temp = 0;
    array <double, arSize> dontation;
    cout << "Enter the following donation amounts\n";
    int i = 0;
    for (i = 0; i < 10; i++)
        {
          cout << "Donation # " << i+1 << " =\t";
          if (!(cin >> dontation [i]))
          {
              cin.clear();
              break;
          }
          else
            temp += dontation[i];
        }
        if (i == 0)
            i++;
        avg = temp / i;
        cout << fixed;
        cout << setprecision(2);
        cout.setf(ios_base::showpoint);
        cout << "Donation total $\t" << temp << endl;
        cout << "The Average is:\t" << avg;

        // Check to see which is the larger amount
        int Larger = 0;
        for (int j = 0; j < i; j++)
        {
            if (dontation[j] > avg)
            {
                cout << "\nThe larger amount #" << j+1 << " is greater than the average.";
                Larger++;
            }
        }
          cout << endl << "The Donations that are larger than the average " << Larger;
return 0;
}
Cool.
Do you have a question?
I was looking to see if there were any suggestions on a different approach to this problem.
like you said there are many ways you could approach this problem the suggestion I would make is to use functions.
and you could make it a bit more clear firstly by doing this:

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
int main()
{
	const int arSize = 10;
	double avg = 0;
	double temp = 0;
	array <double, arSize> dontation;

	cout << "Enter the following donation amounts\n";
	int i = 0;
	for (i = 0; i < 10; i++)
	{
		cout << "Donation # " << i + 1 << " =\t";
		if (!(cin >> dontation[i]))
		{
			cin.clear();
			break;
		}
		else
			temp += dontation[i];
	}
	if (i == 0)
	{
		// dont do anything as you are effectively quitting the program immediately
	}
	else
	{
		avg = temp / i;
		cout << fixed;
		cout << setprecision(2);
		cout.setf(ios_base::showpoint);
		cout << "Donation total $\t" << temp << endl;
		cout << "The Average is:\t" << avg;

		// Check to see which is the larger amount
		int Larger = 0;
		for (int j = 0; j < i; j++)
		{
			if (dontation[j] > avg)
			{
				cout << "\nThe larger amount #" << j + 1 << " is greater than the average.";
				Larger++;
			}
		}
		cout << endl << "The Donations that are larger than the average " << Larger;
	}
		
	
	return 0;
}
1
2
3
if (i == 0)
            i++;
        avg = temp / i;


I thought that by i++ I was incrementing to get to the average which is the next line of code. I am so confused but just trying to understand...
no, if i is zero it implies your user has typed a non-numeric on the very first donation. So there is no value and therefore no average to calculate.

also:

 
 avg = temp / i;


is integer division, which you don't want. you want to cast the denominator to a double.
take a look at this, i've added some comments too.

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
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <iomanip>
#include <array>
using namespace std;

int main()
{
	const int arSize = 10;
	double avg = 0;

	// Running sum of all donations so far.
	double sumOfDonations = 0;
	
	// Because you're using an array of a fixed declared size
	// it's good to keep a track of the actual number of donations
	// made just in case the user doesn't make the full 10 donations.
	int numOfDonations = 0;

	array <double, arSize> dontation;

	cout << "Enter the following donation amounts\n";
	
	for (int i = 0; i < 10; i++)
	{
		cout << "Donation # " << i + 1 << " =\t";
		if (!(cin >> dontation[i]))
		{
			cin.clear();
			break;
		}
		else
		{
			sumOfDonations += dontation[i];
			numOfDonations++;
		}		
	}

	if (numOfDonations == 0)
	{
		// dont do anything as you are effectively quitting the program immediately (no donations)
	}
	else
	{
		avg = sumOfDonations / static_cast<double>(numOfDonations);
		cout << fixed;
		cout << setprecision(2);
		cout.setf(ios_base::showpoint);
		cout << "Donation total $\t" << sumOfDonations << endl;
		cout << "The Average is:\t" << avg;

		// Check to see which is the larger amount
		int Larger = 0;
		for (int j = 0; j < numOfDonations; j++)
		{
			if (dontation[j] > avg)
			{
				cout << "\nThe larger amount #" << j + 1 << " is greater than the average.";
				Larger++;
			}
		}
		cout << endl << "The Donations that are larger than the average " << Larger;
	}


	return 0;
}
Last edited on
Ok cool. Thanks
Topic archived. No new replies allowed.