Here is the revised code-
Code:
#include <iostream>
#include <iomanip>
#include <math.h>
#include <cmath>
double getAmount(double); //function 1
double roundAmount(double); //function 2
double calcAverage_exact(double, int); //function 3.1
double calcAverage_round(int, int); //function 3.2
double calcStdDev_exact(double, double, int); //function 4.1
double calcStdDev_round(int, int, int); //function 4.2
double calcDiff(double, double); //function 5
using namespace std;
int main()
{
double money;
int round_money;
double exact_sum = 0;
double in_sum;
int round_sum = 0;
double exact_squares = 0;
int round_squares;
int count_money = 0;
double sumsquares_exact = 0;
int sumsquares_round = 0;
cout<< "\nEnter an amount (0 to quit):";
cin>> money;
while(money > 0)
{
count_money ++;
exact_sum += money;
money = getAmount(money);
round_money = (int) roundAmount(money);
round_sum += round_money;
exact_squares = money * money;
sumsquares_exact += exact_squares;
round_squares = round_money * round_money;
sumsquares_round += round_squares;
cout<< "\nEnter an amount (0 to quit) :";
cin>> money;
}
cout<<setiosflags(ios::fixed|ios::showpoint)<<setprecision(2);
double average_exact;
double stddev_exact;
double prct_dif;
int average_round;
int stddev_round;
average_exact = calcAverage_exact(sumsquares_exact, count_money);
average_round = (int) calcAverage_round(sumsquares_round, count_money);
stddev_exact = calcStdDev_exact(exact_sum, sumsquares_exact, count_money);
stddev_round = (int) calcStdDev_round(round_sum, sumsquares_round, count_money);
prct_dif = calcDiff(exact_sum, round_sum);
if(average_exact == -1 or average_round == -1)
cout<<"\nnot enough values to calculate the average";
if(stddev_exact == -1 || stddev_round == -1)
cout<<"\nnot enough values to calculate the standard deviation";
else
cout<<"\nCalculation Exact Round";
cout<<"\n--------------------------------------";
cout<<"\nSum:"
<<setw(17)
<<exact_sum
<<setw(12)
<<round_sum;
cout<<"\nAverage:"
<<setw(13)
<<average_exact
<<setw(12)
<<average_round;
cout<<"\nStd Dev:"
<<setw(13)
<<stddev_exact
<<setw(12)
<<stddev_round;
if (prct_dif <= 2)
{
cout<<"\nIts cost effective!\n";
system ("pause");
return 0;
}
else
cout<<"\nIts not cost effective!\n";
system ("pause");
return 0;
}
// Function 1
double getAmount(double money)
{
if (money > 0)
{
return money;
}
else
if (money < 0)
{
cout<<"\nPlease enter a valid number";
cin>>money;
return money;
}
else
return 0;
}
// Function 2
double roundAmount(double money)
{
double in_sum;
int round_money;
in_sum = (money + 5) / 10;
round_money = (int) in_sum * 10;
return round_money;
}
// Function 3.1
double calcAverage_exact(double exact_sum, int count_money)
{
double average_exact;
if (count_money > 1)
{
average_exact = exact_sum / count_money ;
return average_exact;;
}
else
return -1;
}
// Function 3.2
double calcAverage_round(int round_sum, int count_money)
{
double average_round;
if (count_money > 1)
{
average_round = round_sum / count_money;
return average_round;
}
else
return -1;
}
// Function 4.1
double calcStdDev_exact(double exact_sum, double sumsquares_exact, int count_money)
{
double stddev_exact;
if (count_money > 1)
{
stddev_exact = sqrt((sumsquares_exact - (exact_sum * exact_sum / count_money))/(count_money - 1));
return stddev_exact;
}
else
return -1;
}
//Function 4.2
double calcStdDev_round(int round_sum, int sumsquares_round, int count_money)
{
int stddev_round;
if (count_money > 1)
{
stddev_round = (int) sqrt((sumsquares_round - (round_sum * round_sum / count_money))/(count_money - 1));
return stddev_round;
}
else
return -1;
}
//Function 5
double calcDiff(double exact_sum, double round_sum)
{
double prct_dif;
prct_dif = ((fabs(exact_sum - round_sum))/exact_sum) * 100;
return prct_dif;
}
I'm still working on the line documentation but this code should compile for you guys. For some reason my average functions don't make sense though.