Chris Pollett>Old Classes>PIC 10a, Spring 2000>Hw3>Hw3 Solutions
//page 210. Problem 2
// Program Name: pic10avg.cpp
//
// Purpose: Obtains four numbers from the user and caluclates and
// displays there average and standard deviation
//
// Known Bugs: As is not very extendable to more than 4 numbers
#include<iostream.h>
#include<math.h>
//
//Function prototypes
//
void AvgAndStd(double s1, double s2, double s3, double s4, double& avg,
double& stdev);
// Computes the average and standard deviation of four numbers
//
// Precondition: s1, s2, s3, s4 have been assigned values
//
// Postcondition: avg contains the average of these numbers
// stdev contain the standard deviation
double Average(double s1, double s2, double s3, double s4);
// Computes the average and standard deviation of four numbers
//
// Precondition: s1, s2, s3, s4 have been assigned values
//
// Postcondition: average of numbers is returned
//
double StdDev(double s1, double s2, double s3, double s4, double avg);
// Computes the average and standard deviation of four numbers
//
// Precondition: s1, s2, s3, s4, avg have been assigned values
// and avg is the average of s1, s2, s3, s4
//
// Postcondition: standard deviation is returned
//
// Function name: main()
//
// Purpose: driver program to test AvgAndStd. Asks user to enter
// four numbers and the average and stdard deviation of
// these numbers is then displayed. The user is then
// prompted if he/she want to do calculation again.
//
// Known Bugs: none
int main()
{
double s1, s2, s3, s4, avg, stdev;
char keepGoing;
cout << "Standard Deviation and Average calculator\n\n";
cout << "Enter four numbers and this program will compute\n";
cout << "their and average and standard deviation\n";
do
{
//
//Get Inputs
//
cout << "\nEnter the first number: ";
cin >> s1;
cout << "\nEnter the second number: ";
cin >> s2;
cout << "\nEnter the third number: ";
cin >> s3;
cout << "\nEnter the fourth number:";
cin >> s4;
//
//Calculate Average and Standard deviation
//
AvgAndStd( s1, s2, s3, s4, avg, stdev);
//
//Output Results and prompt if want to do again
//
cout << "\nThe average of those numbers was: " << avg << endl;
cout << "\nThe standard deviation was: " << stdev << endl;
cout << "\nDo you want do this again on some more numbers?(y/n)";
cin >> keepGoing;
}while( keepGoing == 'y' );
return 0;
}
// Function name: AvgAndStd
//
// Purpose: Calculates the average and standard deviation of the four
// numbers s1, s2, s3, s4 it returns the result by call-by-
// reference in avg and stdev
//
// Known Bugs: none
void AvgAndStd(double s1, double s2, double s3, double s4, double& avg,
double& stdev)
{
avg = Average(s1,s2,s3,s4);
stdev = StdDev(s1,s2,s3,s4,avg);
}
// Function name: Average
//
// Purpose: Calculates the average of the four
// numbers s1, s2, s3, s4 and returns this value
//
// Known Bugs: none
double Average(double s1, double s2, double s3, double s4)
{
return (s1+s2+s3+s4)/4;
}
// Function name: StdDev
//
// Purpose: Calculates the standard deviation of the four
// numbers s1, s2, s3, s4 using avg as their average.
// It then returns this value.
//
// Known Bugs: none
double StdDev(double s1, double s2, double s3, double s4, double avg)
{
double diff1= s1-avg, diff2 = s2-avg, diff3=s3-avg, diff4=s4-avg;
return sqrt( (
(diff1*diff1)+(diff2*diff2)+(diff3*diff3)+(diff4*diff4)
)/4 );
}
//
// page 210 problem 3
//
// Program Name: pic10avg.cpp
//
// Purpose: Obtains four numbers from the user and caluclates and
// displays there average and standard deviation
//
// Known Bugs: As is not very extendable to more than 4 numbers
#include<iostream.h>
//
//Function prototypes
//
void compute_coin(int coin_value, int& number, int& amount_left);
// Computes the number of coins of denomination coin_value
// can be subtracted from amount_left. It decreases amount_left
// by this much and sets number to this number of coins.
//
// Precondition: 0 < coin_value < 100, 0 <= amount_left < 100
//
// Postcondition: number has been set equal to the maximum number of coins of
// denomination coin_value cents that can be obtained from
// amount_left cents.
// amount_left has been decrease by the value of the coins, i.e.,
// decreased by number*coin_value.
// Function name: main()
//
// Purpose: program to test compute_coin. Asks user to enter
// an amount and tells what coins to give out for that
// amount of change. Then queries user is wants to change
// another amount
//
// Known Bugs: none
int main()
{
int number, amount_left;
char keepGoing;
cout << "\n** Welcome to the Change Making Program ** \n";
do
{
//
// Prompt and get an amount
//
cout << "\nPlease enter a number between 1 and 99 to make"
<< " change for:\n";
cin >> amount_left;
//
// Check if in allowed range and if so make change
// Otherwie give an error
//
if ( amount_left > 0 && amount_left <100)
{
cout << endl<< amount_left << " cents can be given as\n";
compute_coin(25, number, amount_left);
cout << number << "quarter(s) ";
compute_coin(10, number, amount_left);
cout << number << "dime(s) ";
compute_coin(1, number, amount_left);
cout << number << "penny(pennies) ";
}
else cout << "\nError. Number should be between 1 and 99";
//
// Prompt whether to do again
//
cout << "\n\nDo you want to make change again? (y/n)\n";
cin >> keepGoing;
}
while( keepGoing == 'y');
return 0;
}
// Function name: compute_coin()
//
// Purpose: Computes the number of coins of denomination coin_value
// can be subtracted from amount_left. It decreases amount_left
// by this much and sets number to this number of coins.
//
// Known Bugs: none
void compute_coin(int coin_value, int& number, int& amount_left)
{
number = amount_left/coin_value;
amount_left %= coin_value;
}