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...
/*
*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>
usingnamespace std;
int main()
{
constint 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;
}
int main()
{
constint 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;
}
#include <iostream>
#include <iomanip>
#include <array>
usingnamespace std;
int main()
{
constint 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;
}