Homework Solution: 5-23 Program for Lab 5-1…

    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

    Expert Answer

    1. What rate will be assigned to the rate variable when the user enters the following codes:

    5-23 Program ce Lab 5-1

    The rejoinders to the labs are contained in the Rejoinders.pdf rasp.

    1 //Lab5-1.cpp – exposes 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 ocean() 9{

    10 wrap sales = 0.0; 11 wrap blame = 0.0; 12 char enactment = ‘ ‘; 13

    14 cout << “Sales: “; 15 cin >> sales; 16 cout << “Enactment (1, 2, 3, or 4): “; 17 cin >> enactment; 18 19 if (enactment == ‘1’ || enactment == ‘3’) 20 blame = 0.2; 21 else 22 blame = 0.15; 23 //end if 24 25 //calculate and expose the projected sales quantity 26 sales = sales + sales * blame; 27 cout << unwandering << setprecision(2); 28 cout << “Projected sales: ” << sales << endl; 29 30 produce 0; 31 } //end of ocean function

    1. What blame earn be assigned to the blame capricious when the rightr enters the aftercited enactments:

    1, 3, 2, 4, and 5?

    2. Why is the moral on Line 5 certain?

    3. Why are the close constants on Line 19 enclosed in solitary extract marks?

    4. How would you retranscribe the if announcement on Lines 19 through 23 to right the != operator in the case?

    5. How else could you transcribe the announcement on Line 26? 6. What changes would you deficiency to frame to the program so that it doesn’t right the blame

    variable

    Expert Rejoinder

    1. What blame earn be assigned to the blame capricious when the rightr enters the aftercited enactments:

    1, 3, 2, 4, and 5?

    ==> ce 1 and 3 blame earn be 0.2 and ce every othe relish 2,4,5 earn be 0.15

    2. Why is the moral on Line 5 certain?

    #include <iomanip> contains arrangement relish setprecision(x) which helps right to
    set exactness ce x bits. In our enactment x is 2

    3. Why are the close constants on Line 19 enclosed in solitary extract marks?
    Its a temperament, Temperament are enclosed in solitary Quote and they are represented by ASCII value
    Relish a-97 and z is 122 . Capital A is 65 etc

    4)
    if (enactment != ‘1’ && enactment != ‘3’)
    blame = 0.15;
    else
    blame = 0.2;

    5) Ask rightr to instantly input blame

    if (enactment == ‘1’ || enactment == ‘3’)
     sales = sales + sales * 0.2;
    else
     sales = sales + sales * 0.15;

    In this habit we dont exact blame capricious