5-23 Program for Lab 5-1 The answers to the labs are contained in the Answers.pd
ID: 3882440 • Letter: 5
Question
5-23 Program for Lab 5-1
The answers to the labs are contained in the Answers.pdf file.
1 //Lab5-1.cpp - displays projected sales 2 //Created/revised by <your name> on <current date> 3 4 #include <iostream> 5 #include <iomanip> 6 using namespace std; 7 8 int main() 9{
10 double sales = 0.0; 11 double rate = 0.0; 12 char code = ' '; 13
14 cout << "Sales: "; 15 cin >> sales; 16 cout << "Code (1, 2, 3, or 4): "; 17 cin >> code; 18 19 if (code == '1' || code == '3') 20 rate = 0.2; 21 else 22 rate = 0.15; 23 //end if 24 25 //calculate and display the projected sales amount 26 sales = sales + sales * rate; 27 cout << fixed << setprecision(2); 28 cout << "Projected sales: " << sales << endl; 29 30 return 0; 31 } //end of main function
1. What rate will be assigned to the rate variable when the user enters the following codes:
1, 3, 2, 4, and 5?
2. Why is the directive on Line 5 necessary?
3. Why are the literal constants on Line 19 enclosed in single quotation marks?
4. How would you rewrite the if statement on Lines 19 through 23 to use the != operator in the condition?
5. How else could you write the statement on Line 26? 6. What changes would you need to make to the program so that it doesn’t use the rate
variable
Explanation / Answer
1. What rate will be assigned to the rate variable when the user enters the following codes:
1, 3, 2, 4, and 5?
==> for 1 and 3 rate will be 0.2 and for all othe like 2,4,5 will be 0.15
2. Why is the directive on Line 5 necessary?
#include <iomanip> contains method like setprecision(x) which helps use to
set precision for x bits. In our code x is 2
3. Why are the literal constants on Line 19 enclosed in single quotation marks?
Its a character, Character are enclosed in single Quote and they are represented by ASCII value
Like a-97 and z is 122 . Capital A is 65 etc
4)
if (code != '1' && code != '3')
rate = 0.15;
else
rate = 0.2;
5) Ask user to directly input rate
if (code == '1' || code == '3')
sales = sales + sales * 0.2;
else
sales = sales + sales * 0.15;
In this way we dont require rate variable
Thanks, let me know if there is any doubts/concern.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.