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

16. In this exercise, you explore the use of integers in monetary calculations.

ID: 3909147 • Letter: 1

Question

16. In this exercise, you explore the use of integers in monetary calculations. a. Follow the instructions for starting C++ and opening the Advanced16.cpp file. Run the program. Enter 256.7 and 223.3 as the sales for Store 1 and Store 2, respectively. The total that appears on the computer screen (504.00) is incorrect because it is not the result of adding together the numbers 269.54 and 234.47. Press any key to stop the program. b. Review the code contained in the Advanced16.cpp file. The #include directive tells the C++ compiler to include the contents of the iomanip file in the current program. The file contains the definition of the setprecision stream manipulator,whichappearsinthecout << fixed << setprecision(2) << endl; statement. The fixed stream manipulator, which is defined in the iostream file, forces a real number to display a specific number of decimal places, as specified by the setprecision stream manipulator. In this program, the output values will display with two decimal places. You will learn about the directive and both stream manipulators in Chapter 5. c. Why does the total appear as 504.00 rather than 504.01? Hint: Change the cout << fixed << setprecision(2);statementtoacomment,andthensaveandrun the program. Enter 256.7 and 223.3 as the sales for Store 1 and Store 2, respectively. Study the output, and then stop the program. Change the comment back to a statement. d. Use the seven comments that appear below the main function to modify the program’s code. Why do you need to add .5 to the expressions that calculate the increased sales for both stores?

//Advanced16.cpp //Created/revised by on #include #include using namespace std; int main() { //declare constant and variables const double increase = 1.05; double store1 = 0.0; double store2 = 0.0; double total = 0.0; cout << "Store 1 sales: "; cin >> store1; cout << "Store 2 sales: "; cin >> store2; store1 = store1 * increase; store2 = store2 * increase; total = store1 + store2; cout << fixed << setprecision(2) << endl; cout << store1 << " --->Store 1" << endl; cout << store2 << " --->Store 2" << endl; cout << "-----------------" << endl; cout << total << " --->Total" << endl; return 0; } //end of main function //1. Declare and initalize three int variables named store1Int, store2Int, and totalInt //2. Change the first assignment statement to store1Int = static_cast(store1 * increase * 100 + .5); //3. Change the second assignment statement to store2Int = static_cast(store2 * increase * 100 + .5); //4. Change the third assignment statement to totalInt = store1Int + store2Int; //5. Change store1 in the second cout statement to store1Int / 100.0 //6. Change store2 in the third cout statement to store2Int / 100.0 //7. Change total in the last cout statement to totalInt / 100.0

Explanation / Answer


Given below is the modified code for the question.
Please do rate the answer if it was helpful. Thank you

While calculating the increased sales, 0.5 is added so that the integer is rounded off correctly to either floor / ceiling of the number.

//Advanced16.cpp
//Created/revised by on
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
//declare constant and variables
const double increase = 1.05;
double store1 = 0.0;
double store2 = 0.0;
double total = 0.0;
//1. Declare and initalize three int variables named store1Int, store2Int, and totalInt
int store1Int, store2Int, totalInt;

cout << "Store 1 sales: ";
cin >> store1;
cout << "Store 2 sales: ";
cin >> store2;
//2. Change the first assignment statement to store1Int = static_cast(store1 * increase * 100 + .5);
store1Int = static_cast<int>(store1 * increase * 100 + .5);

//3. Change the second assignment statement to store2Int = static_cast(store2 * increase * 100 + .5);
store2Int = static_cast<int>(store2 * increase * 100 + .5);

//4. Change the third assignment statement to totalInt = store1Int + store2Int;
totalInt = store1Int + store2Int;
cout << fixed << setprecision(2) << endl;

//5. Change store1 in the second cout statement to store1Int / 100.0
//6. Change store2 in the third cout statement to store2Int / 100.0
//7. Change total in the last cout statement to totalInt / 100.0

cout << store1Int / 100.0 << " --->Store 1" << endl;
cout << store2Int / 100.0 << " --->Store 2" << endl;
cout << "-----------------" << endl;
cout << totalInt / 100.0 << " --->Total" << endl;


return 0;

} //end of main function

output
======
Store 1 sales: 500
Store 2 sales: 900

525.00 --->Store 1
945.00 --->Store 2
-----------------
1470.00 --->Total

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote