/****************************************************************** CSCI 240 Assignment 6 Part 1 Fall 2025 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 #include #include using namespace std; //Function Prototypes double getExact(); 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 by calling the getExact //* function. The value returned from the function should be saved in //* the exactAmt variable. //* //********************************************************************* //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 by calling the roundAmount function. The value returned from //* the function should be saved in the roundedAmt variable. //* //********************************************************************* //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 cnt++; //********************************************************************* //* //* Fill In The Blank 3: //* //* Get the next EXACT dollar amount from the user by calling the getExact //* function. The value returned from the function should be saved in //* the exactAmt variable. //* //********************************************************************* } //closing curly brace for the while loop //********************************************************************* //* //* Fill In The Blank 4: //* //* Using the exactSum and cnt variables, calculate the average for the //* EXACT dollar amounts by calling the calcAverage function. The value //* returned from the function should be saved in the exactAvg variable. //* //********************************************************************* //********************************************************************* //* //* Fill In The Blank 5: //* //* Using the roundedSum and cnt variables, calculate the average for the //* ROUNDED dollar amounts by calling the calcAverage function. The value //* returned from the function should be saved in the roundedAvg variable. //* //********************************************************************* //********************************************************************* //* //* Fill In The Blank 6: //* //* Using the exactSum, exactSumSquared and cnt variables, calculate the //* standard deviation of the EXACT dollar amounts by calling the calcStdDev //* function. The value returned from the function should be saved in //* the exactSD variable. //* //********************************************************************* //********************************************************************* //* //* Fill In The Blank 7: //* //* Using the roundedSum, roundedSumSquared and cnt variables, calculate //* the standard deviation of the ROUNDED dollar amounts by calling the //* calcStdDev function. The value returned from the function should be //* saved in the roundedSD variable. //* //********************************************************************* //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 << 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 dollar //amounts have been entered. Otherwise, display an error message if (cnt > 0) { //********************************************************************* //* //* Fill In The Blank 8: //* //* Using the exactSum and roundedSum variables, calculate the percent //* difference between the EXACT and ROUNDED sums by calling the //* calcDiff function. The value returned from the function should be //* saved in the percentDiff variable. //* //********************************************************************* 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"; } cout << endl; return 0; }/* end of main */ /****************************************************************** * * getExact * * This function gets an exact dollar amount from the user. It * makes sure the value is 0 or positive. * * @param None * * @return The exact dollar amount - a value that is either 0 or * positive * ******************************************************************/ double getExact() { //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; } /****************************************************************** * * roundAmount * * This function rounds a value to the nearest $10 dollar amount * * @param amount the value to be rounded * * @return 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; } /****************************************************************** * * calcAverage * * This function calculates an average. * * @param sum the sum of all of the values * @param count the number of values * * @return 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; } } /****************************************************************** * * calcStdDev * * This function calculates a standard deviation. * * @param sum the sum of all of the values * @param sumOfSquares the sum of all of the squared values * @param count the number of values * * @return 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)); } } /****************************************************************** * * calcDiff * * This function calculates a percent difference * * @param exactSum - the sum of all of the EXACT values * @param roundedSum - the sum of all of the ROUNDED values * * @return 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; }