Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Overview For this assignment, fill in the blanks of a pre-written program with c

ID: 3529284 • Letter: O

Question

Overview For this assignment, fill in the blanks of a pre-written program with calls to various functions. Program Description The pre-written program can be found here: http://faculty.cs.niu.edu/~byrnes/csci240/pgms/240pgm5pt1.cpp This program will test the effect of rounding dollar amounts to the nearest $10 rather than keeping track of exact amounts. It has been determined that if the difference between the results of the two methods (keeping track of exact values and keeping track of rounded values) is less than 2%, then it would be cost effective for a company to round rather than balance to the penny. The program will accept dollar amounts from the user until 0 is entered. Each exact dollar amount is then rounded to the nearest $10 value. The exact and rounded amounts are then used to keep track of: the sum of the exact dollar amounts the sum of the rounded dollar amounts the sum of the squares of the exact dollar amounts the sum of the squares of the rounded dollar amounts the number of dollar amounts entered After the user has entered an exact amount of 0, the sum, average, and standard deviation for both the exact and rounded amounts are displayed. The percent difference between the sums of the exact and rounded amounts is calculated and a determination is made as to whether or not it's cost effective to round the dollar values. The Functions The following functions have been written: double getAmount() The getAmount funtion takes no arguments and returns a "valid" amount. Its purpose is to "get" an exact dollar amount from the user and verify that it is either 0 or positive. If the value entered is valid, it is returned to the calling function. If the value entered is invalid, an error message is displayed and the user is prompted for a new value. This is done until a valid value is entered. double roundAmount( double amount ) The roundAmount function takes one double argument: a dollar amount to be rounded to the nearest $10 value and returns a double: the rounded dollar amount. It rounds the exact dollar amount by: adding 5 to the exact amount dividing by 10 multiplying the integer result by 10. double calcAverage( double sum, int count ) The calcAverage function takes two arguments: a double value that represents a sum of dollar amounts, and an integer value that represents a count of dollar amounts. Its purpose is to calculate and return an average if the count is greater than 0. If the count is less than or equal to 0, -1 is returned. double calcStdDev( double sum, double sumOfSquares, int count ) The calcStdDev function takes three arguments: a double value that represents a sum of dollar amounts, a double value that represents the sum of the squared dollar amounts, and an integer value that represents a count of dollar amounts. The purpose of this function is to calculate and return the standard deviation if the count is greater than 1. If the count is less than 1, -1 is returned. The formula for standard deviation is: where n is the number of entries (count). double calcDiff( double exactSum, double roundedSum ) The calcDiff function takes two arguments: a double value that represents a sum of the exact dollar amounts, and a double value that represents the sum of the rounded dollar amounts. The purpose of this function is to calculate and return the percent difference between the sum of the exact values and the sum of the rounded values. The percent difference formula is: The fabs() function is used to find the absolute value. Program Requirements DO NOT WRITE ANY FUNCTIONS. Simply go through the program that has been provided and put in function calls where indicated. For example: //********************************************************************* //* //* Fill In The Blank 1: //* //* Get the first EXACT dollar amount from the user. The value should //* be saved in the exactAmt variable. //* //********************************************************************* This box should be followed by a call to the getAmount function that is described above. There are 8 boxes similar to this in the program, which means 8 function calls need to be be added. The float/double results will be displayed with exactly 2 digits after the decimal point, including zeros.

Explanation / Answer

please rate - thanks


/******************************************************************

CSCI 240 Program 5 Part 1 Spring 2013


Programmer:


Section:


Date Due:


Purpose: This program uses functions to test the effects of

rounding to the nearest $10.


It is an exercise in learning to call functions.

******************************************************************/


#include <iostream>

#include <iomanip>

#include <math.h>


using namespace std;


//Function Prototypes


double getAmount();

double roundAmount( double );

double calcAverage( double, int );

double calcStdDev( double, double, int );

double calcDiff( double, double );



int main()

{

double exactAmt, /* exact dollar amount */

roundedAmt, /* amount rounded to nearest $10 */

exactSum = 0.0, /* sum of exact dollar amounts */

exactSumSquared = 0.0, /* sum of squared exact dollar amounts */

roundedSum = 0.0, /* sum of rounded dollar amounts */

roundedSumSquared = 0.0, /* sum of squared rounded dollar amount */

exactAvg, /* average of exact dollar amounts */

roundedAvg, /* average of rounded dollar amounts */

exactSD, /* standard deviation of exact amounts */

roundedSD, /* standard deviation of rounded amounts */

percentDiff; /* % difference between exact & rounded */


int cnt = 0; /* number of dollar amounts entered */




//*********************************************************************

//*

//* Fill In The Blank 1:

//*

//* Get the first EXACT dollar amount from the user. The value should

//* be saved in the exactAmt variable.

//*

//*********************************************************************


exactAmt=getAmount();




//While the user is still entering dollar amounts


while (exactAmt != 0.00)

{


//*********************************************************************

//*

//* Fill In The Blank 2:

//*

//* Round the user's EXACT dollar amount to the nearest ten dollar

//* amount. The value should be saved in the rounded variable.

//*

//*********************************************************************


roundedAmt=roundAmount(exactAmt);



//Calculate sums for the EXACT and ROUNDED amounts


exactSum += exactAmt;

exactSumSquared += (exactAmt * exactAmt);


roundedSum += roundedAmt;

roundedSumSquared += (roundedAmt * roundedAmt);



//Increment the count of dollar amounts entered and get the next EXACT amount


cnt++;



//*********************************************************************

//*

//* Fill In The Blank 3:

//*

//* Get the next EXACT dollar amount from the user.

//*

//*********************************************************************



exactAmt=getAmount();



} //closing curly brace for the while loop



//*********************************************************************

//*

//* Fill In The Blank 4:

//*

//* Using the exactSum and cnt, calculate the average for the EXACT

//* dollar amounts. The value should be saved in the exactAvg variable.

//*

//*********************************************************************



exactAvg=calcAverage(exactSum,cnt);



//*********************************************************************

//*

//* Fill In The Blank 5:

//*

//* Using the roundedSum and cnt, calculate the average for the ROUNDED

//* dollar amounts. The value should be saved in the roundedAvg variable.

//*

//*********************************************************************


roundedAvg=calcAverage(roundedSum,cnt);




//*********************************************************************

//*

//* Fill In The Blank 6:

//*

//* Using the exactSum, exactSumSquared and cnt, calculate the standard

//* deviation of the EXACT dollar amounts. The value should be saved in

//* the exactSD variable.

//*

//*********************************************************************



exactSD=calcStdDev(exactSum,exactSumSquared,cnt);



//*********************************************************************

//*

//* Fill In The Blank 7:

//*

//* Using the roundedSum, roundedSumSquared and cnt, calculate the

//* standard deviation of the ROUNDED dollar amounts. The value should

//* be saved in the roundedSD variable.

//*

//*********************************************************************


roundedSD=calcStdDev(roundedSum,roundedSumSquared,cnt);




//Display the calculated sums, averages and standard deviations for both

//the exact and rounded values or an error message if the average or

//standard deviation could not be calculated.


cout << fixed << showpoint << setprecision(2);


cout << endl << "Calculation Exact Rounded"

<< endl << "------------------------------------------"

<< endl << "Sum:" << setw(21) << exactSum << setw(17) << roundedSum;

if ( exactAvg == -1 ) // could also add || roundAvg == -1

cout << endl << "Average: *** could not be calculated ***";

else

cout << endl << "Average:" << setw(17) << exactAvg << setw(17) << roundedAvg;

if ( exactSD == -1 ) // could also add || roundedSD == -1

cout << endl << "Std Dev: *** could not be calculated ***";

else

cout << endl << "Std Dev:" << setw(17) << exactSD << setw(17) << roundedSD;




//Calculate the percent difference and display the results



if (cnt > 0)

{

//*********************************************************************

//*

//* Fill In The Blank 8:

//*

//* Using the exactSum and roundedSum, calculate the percent difference

//* between the EXACT and ROUNDED sums. The value should be saved in

//* the percentDiff variable.

//*

//*********************************************************************

percentDiff=calcDiff(exactSum,roundedSum);





cout << endl << endl << "The difference is " << percentDiff << ". Rounding is ";

if (percentDiff < 2.0)

cout << "acceptable";

else

cout << "unacceptable";

}

else

{

cout << endl << endl << "The difference cannot be calculated";

}


return 0;

}/* end of main */



/******************************************************************


Function: getAmount


Use: This function gets an exact dollar amount from the user. It

makes sure the value is 0 or positive.


Arguments: None


Returns: The exact dollar amount - a value that is either 0 or

positive


******************************************************************/


double getAmount()

{

//Create a variable to hold the amount that is entered by the user

double amount;



//Get a positive dollar amount (or 0) from the user

cout << "Enter a positive dollar amount or 0 to quit: ";

cin >> amount;


//While the user's amount is invalid, get a new one

while ( amount < 0 )

{

//Display an error message about an invalid amount and get a new one

cout << "Error: amount must be greater than or equal to 0. Try again: ";

cin >> amount;

}//end of while



//Return the valid dollar amount


return amount;

}



/******************************************************************


Function: roundAmount


Use: This function rounds a value to the nearest $10 dollar amount


Arguments: amount - the value to be rounded


Returns: The rounded amount


******************************************************************/


double roundAmount( double amount )

{

//Calculate the amount rounded to the nearest $10 amount and

//return the result


return ((int) ((amount + 5) / 10)) * 10;

}



/******************************************************************


Function: calcAverage


Use: This function calculates an average.


Arguments: sum - the sum of all of the values


count - the number of values


Returns: The calculated average or -1 if the average cannot be

calculated


******************************************************************/


double calcAverage(double sum, int count)

{

//If the count is 0 or negative, return the error code of -1;

//Otherwise, calculate the average and return the result


if( count < 1 )

return -1;


else

return sum / count;

}



/******************************************************************


Function: calcStdDev


Use: This function calculates a standard deviation.


Arguments: sum - the sum of all of the values


sumOfSquares - the sum of all of the squared values


count - the number of values


Returns: The calculated standard deviation or -1 if the standard

deviation cannot be calculated


******************************************************************/


double calcStdDev( double sum, double sumOfSquares, int count )

{

//If the count is less than or equal to 1, return the error code of -1;

//Otherwise, calculate the standard deviation and return the result


if( count <= 1 )

return -1;

else

return sqrt ((sumOfSquares - (sum * sum / count)) / (count - 1));

}



/******************************************************************


Function: calcStdDev


Use: This function calculates a percent difference


Arguments: exactSum - the sum of all of the EXACT values


roundedSum - the sum of all of the ROUNDED values


Returns: The calculated percent difference


Note: The fabs function is used to calculate an absolute value


******************************************************************/


double calcDiff( double exactSum, double roundedSum )

{

//Calculate the percent difference and return the result


return ((fabs(exactSum - roundedSum)) / exactSum) * 100;

}