Homework Week 6 Done
Homework Week 6 Done
1. Write the c++ code for the multiple-alternative selection structure shown in figure 6-35.
First, use the longer form of the I f statement. Then rewrite the code using the shorter form
of the I f statement.
If (sales < = 0)
Bonus = 0
Display “the sales mus be greater than 0”
Else
if (sales <= 1000)
Bonus =sales * 0.15
Else
If (sales <= 5000)
Bonus = sales * 0.20
Else
Bonus = sales * 0.25
End if
End if
End if
Answer
if (sales <= 0)
cout << "the sales must be greater than 0." << endl ;
else if (sales <= 1000)
bonus = sales * 0.15 ;
else if ( sales <= 5000)
bonus = sales * 0.2 ;
else if (sales >= 5001)
bonus = sales * 0.25 ;
#include <iostream>
using namespace std;
int main()
{
double sales = 0.0 ;
double bonus = 0.0 ;
if (sales <= 0)
cout << "the sales must be greater than 0." << endl ;
else if (sales <= 1000)
bonus = sales * 0.15 ;
else if ( sales <= 5000)
bonus = sales * 0.2 ;
else if (sales >= 5001)
bonus = sales * 0.25 ;
//end if
cout << "bonus is : $ "<< bonus << endl ;
return 0;
}
2. Using the s w I t c h statement, write the C++ code that corresponds to the partial flowchart
shown in figure 6.36. use a char variable named code and a double variable named rate.
code
e
1 2,3,4 5 6,7 other
Figure 6-36
#include <iostream>
using namespace std;
int main()
{
char code = ' ' ;
double rate = 0.0 ;
switch (code)
{
case '1' :
rate = 0.2 ;
break;
case '2' :
case '3' :
case '4' :
rate = 0.05 ;
break ;
case '5' :
rate = 0.1 ;
break ;
case '6' :
case '7' :
rate = 0.15 ;
break ;
default :
rate = -1 ;
}
cout << "CODE : " << code << endl;
cout << "RATE : " << rate << endl;
return 0 ;
}
3. Complete exercise 2, and then change the switch statement to the multiple-alternative form
of the If statement.
#include <iostream>
using namespace std;
int main()
{
char code = ' ' ;
double rate = 0.0 ;
if (code == '1')
rate = 0.2 ;
else if (code=='2'||code=='3'||code=='4')
rate = 0.05 ;
else if (code=='5')
rate = 0.1 ;
else if (code=='6'||code=='7')
rate = 0.15 ;
else
rate = -1 ;
return 0 ;
}
4. Travis is standing in front of two containers : one marked trash and other marked recycle. In
his right hand, he is holding a bag that contains either trash or recyclables. Travis needs to
lift the lid from the appropriate container (if necessary). Then drop the bag in the container,
and then put the lid back on the container. Write an appropriate algorithm, using only the
instructions listed in figure 6-37. (an instructions can be used more than once.)
else
end if
drop the bag of recyclables in the Recycle container
drop the bag of trash in the Trash container
if (the bag contains trash)
if (the lid is on the Recycle container)
if (the lid is on the Trash container)
lift the Recycle container’s lid using your left hand
lift the Trash container’s lid using your left hand
put the lid back on the Recycle container using your left
hand
put the lid back on the Trash container using your left hand
Figure 6-37
Answer
if (the bag contains trash)
if (the lid is on the trash container)
lift the trash container`s lid using your left hand
drop the bag of trash in the trash container
put the lid back on the trash container using your left
hand
else
if (the lid is on the recycle container)
lift the recycle container`s lid using your left hand
drop the bag of recyclables in the recycle container
put the lid back on the recycle container using your
left hand
end if
5. Write the C++ code for a multiple-alternative selection structure that displays one of
four different messages, depending on the number of seminar participants stored in
an int variable named participants. Display the message “Use the Kanton room”
when the number of seminar participants is at least 75. When the number is 40 through
74, display the message “Use the Harris room”. When the number is 10 through 39,
display the message “Use the small conference room”. When the number is less than 10,
display the message “Cancel the seminar”.
#include <iostream>
using namespace std;
int main()
{
int room = 0;
return 0;
}
6. A program stores test scores in two int variables named myScore and expectedScore.
Write the C++ code to compare the two scores and then display one of the following messages:
“I met my expectations”, “I scored higher than expected”, or “I scored lower than expected”.
#include <iostream>
using namespace std;
int main()
{
int myScore = 0 ;
int expectedScore= 0 ;
if (myScore == expectedScore)
cout << "I meet my expections: " ;
else if (myScore >= expectedScore )
cout << "I scored higher than expected" ;
else if (myScore <= expectedScore)
cout << "I scored lower than expected" ;
return 0 ;
}
7. A program uses a char variable named department and two double variables named salary and
raise. The department variable contains one of the following letters (entered in either uppercase
or lowercase): A, B, C. or D. employees in department A and B are receiving a 2% raise.
Employees in department C are receiving a 1.5% raise, and employees in department D are
receiving a 3% raise. Using the switch statement, write the C++ code to calculate the appropriate
raise amount.
#include <iostream>
using namespace std;
int main()
{
char depart = ' ';
double raise = 0.0 ;
switch ((toupper)(depart))
{
case 'A':
case 'B':
raise = 0.02 ;
break ;
case 'C' :
raise = 0.015 ;
break ;
case 'D' :
raise = 0.03 ;
break;
default :
cout << "error" << endl ;
break;
}
cout << "raise amount is :" << raise << endl ;
return 0 ;
}
8. A program uses a char variable named membership and an int variable named age. The
membership variable contains one of the following letters (entered in either uppercase or
lowercase) : M or N. the letter M stands for member and the N stands for non-member. The
program should display the appropriate seminar fee, which is based on a person`s membership
status and age. The fee schedule is shown in figure 6-38. Write the C++ code to display the fee.
#include <iostream>
using namespace std;
int main()
{
char membership = ' ';
int age = 0 ;
int semFee = 0 ;
if (age <=65)
semFee = 10 ;
else
semFee = 5 ;
if (age<=65)
semFee=20 ;
else
semFee=15;
return 0 ;
}
9. The C++ code in figure 6-39 should display one of the four messages listed in the figure. The
appropriate message is based on the level entered by the user. Correct the errors in the code.
Level Message
1 or 2 Bronze
3 Silver
4 or 5 Gold
other Invalid ID
Int level = 0;
Cout << “level (1through 5) : “ ;
Cin >> level ;
Swith (level)
{
Case 1:
Case 2:
cout << “bronze” ;
case 3:
break ;
cout << “silver “ ;
case 4:
cour << “gold” ;
default:
cout << “invalid ID” ;
} //end switch
#include <iostream>
using namespace std;
int main ()
{
int level = 0 ;
cout << "Enter your level: " ;
cin >> level ;
switch (level)
{
case 1 :
case 2 :
cout <<"BRONZE" ;
break;
case 3 :
cout << "SILVER" ;
break ;
case 4 :
case 5 :
cout << "GOLD" ;
break;
default:
cout <<"invalid ID" ;
break;
}
cout << " IS YOUR LEVEL " << endl ;
return 0 ;
}
10. Figure 6-40 shows a partially completed chart for a program that displays the amount of a
salesperson`s commission. The commission is based on the salesperson`s sales amount, as
indicated in the figure. Complete the selection structure in the algorithm section of the chart.
Also complete the C++ instructions section. After completing the chart, create a new project (if
necessary) named project, and save it in the folder. Enter the C++ instructions into a source file.
Also enter appropriate comments and any additional instructions required by the compiler.
Display the commission in fixed-point notation with two decimal places. Save and run the
program. Test the program using the following sales amount: 15250, 251990, 500670, and -5.
Processing
None
Output
Commission Double commission = 0.0 ;
if (sales < 0)
commission = -1 ;
else if ( sales <= 100000 )
commission = sales*0.02 ;
else if (sales <= 400000)
commission = sales*0.05 ;
else if (sales >= 400001)
commission = sales * 0.09;
{
if (commission !=-1)
{
cout << fixed << setprecision (2) ;
cout << commission << endl;
}
else
{
cout << "sales cannot be less than 0" ;
}
}
return 0 ;
}
11. Code the algorithm shown in figure 6-41. Use the switch statement to code the multiple-
alternative selection structure. If necessary, create a new project named project, and save it in
the folder. Enter the C++ instructions into a source file named. Also enter appropriate comments
and any additional instructions required by the compiler. Save and run the program. Test the
program using the following codes : 1, 2, 3, and 7.
Processing
None
Output
Salary Int salary = 0 ;
Algorithm
1. Enter the department code Cout << “enter the department code” ;
2. If (the department code is one of the following) Cin >> deptCode ;
1. Salary = 25000
2. Salary = 30000 switch (deptCode)
3. Salary = 32000 {
4. Display “invalid code” case 1 :
Salary = 0 salary = 25000 ;
End if break ;
3. Display salary case 2 :
salary = 30000 ;
break ;
case 3 :
salary = 32000 ;
break ;
default :
salary = 0 ;
break ;
}
Cout << “Salary is : $ “ << salary <<
endl;
DESK-CHECK
DEPARTMENT CODE SALARY
1 25000
2 30000
3 32000
7 0
#include <iostream>
using namespace std;
int main()
{
int deptCode = 0;
int salary = 0 ;
switch (deptCode)
{
case 1 :
salary = 25000 ;
break ;
case 2 :
salary = 30000 ;
break ;
case 3 :
salary = 32000 ;
break ;
default :
salary = 0 ;
break ;
}
cout << "Salary is : $ " << salary << endl;
}
12. Complete exercise 11. If necessary, create a new project named, and save it in the folder. Enter
(or copy) the instructions form the file into a new source file. Be sure to change the filename in
the first comment. Remove the default clause from the switch statements. Modify the code to
verify that the department code is 1, 2, or 3 only. If the department code is valid, use the switch
statement to determine the salary. And then display the salary. If the department code is not
valid, use the switch statement to determine the salary, and then display the salary. If the
department code is not valid, display the “invalid code” message. Save and run the program. Test
the program using the following codes : 1, 2, 3, and 7.
Processing
None
Output
Salary Int salary = 0 ;
Algorithm
1. Enter the department code Cout << “enter the department code” ;
2. If (the department code is one of the following) Cin >> deptCode ;
Salary = 25000
Salary = 30000 If (deptCode = 1 )
Salary = 32000 Salary = 25000 ;
Display “invalid code” Else if (deptCode = 2)
Salary = 0 Salary = 30000 ;
End if Else if (deptCode = 3)
3. Display salary Salary = 32000 ;
Else if ;
Cout << “invalid code” ;
DESK-CHECK
DEPARTMENT CODE SALARY
1 25000
2 30000
3 32000
7 Invalid code
#include <iostream>
using namespace std;
int main()
{
int deptCode = 0;
int salary = 0 ;
if (deptCode == 1 )
salary = 25000 ;
else if (deptCode == 2)
salary = 30000 ;
else if (deptCode == 3)
salary = 32000 ;
else
salary = 0 ;
cout << "invalid code" << endl ;
}
13. Kariton learning wants a program that displays the amount of money a company owes for a
seminar. The fee per person is based on the number of people the company registers, as shown
in figure 6-42. For example, if the company registers seven people, the total amount owed is
$700. If the user enters a number that is less than or equal to 0, the program should display an
appropriate error message.
1 through 5 $125
6 through 20 $100
21 or more $ 75
a. Create an IPO chart for the problem and then desk-check the algorithm five times, using the
number 4, 8, 22, 0 and -2 as the number of people registered.
b. List the input, processing and output items, as well as the algorithm, in a chart similar to the one
shown earlier in figure 6-27. Then code the algorithm into a program.
c. Desk-check the program using the same data used to desk-check the algorithm.
Processing
None
Output
Fee per person Int perPerson = 0 ;
Total amount owed Double totalAmount = 0.0 ;
Algorithm
1. Enter number of registrants Cout << “enter number of registrants” ;
2. If Cin >> numRegis ;
1 through 5
Fee per person = $125 If (numRegis >= 1 *&& numRegis <=5)
Else perPerson = 125 ;
If 6 through 20 else if (numRegis >= 6 *&& numRegis <=20)
Fee per person = $100 perPerson = 100;
Else else if (numRegis >= 21)
If 21 or more perPerson = 75
Fee per person = $75 else if ( numRegis <=0)
Else cout << “appropriate error message” << endl ;
Display appropriate error message
3. Calculated total amount owed by
multiplying number of registrants by totalAmount = numRegis * perPerson ;
fee per person
4. Display fee per person and total cout << “fee per person is : $ “ << perPerson << endl;
amount owed cout << "total amount owed is: $ "<< totalAmount <<
endl;
#include <iostream>
using namespace std;
int main ()
{
int numRegis = 0 ;
int perPerson = 0 ;
double totalAmount = 0.0;
cout << "enter number of registrants : " ;
cin >> numRegis ;
cout << "fee per person is : $ " << perPerson << endl;
cout << "total amount owed is: $ "<< totalAmount << endl;
return 0 ;
}
a. Create an IPO chart for the problem, and then desk-check the algorithm seven times, using
sales of 20000, 20001, 30000, 50000, 50001, 75000 and -3.
b. List the input, processing and output items, as well as the algorithm, in a chart similar to the
one shown earlier in figure 6-27. Then code the algorithm into a program .
c. Desk-check the program using the same data used to desk-check the algorithm.
Processing
None
Output
Commission Double commission = 0.0;
Algorithm
1. Enter number of quarterly sales Cout<< “enter number of quarterly sales”;
2. If quarterly sales <=20000 Cin >> sales;
Calculated commission by multiplying if (sales>0&&sales<=20000)
sales by 0.05. commission = (sales*0.05);
Else if else if (sales>=20001 && sales
Sales more than 20001 less than <=50000)
50000. commission= (sales*0.07)+1000;
Calculated commission by multiplying else if (sales>=50001)
sales by 0.07 and adding 1000 commission=(sales*0.1)+3100;
Else else
Sales more 50001 cout << "error";
Calculated commission by multiplying
sales by 0.10 and adding 3100.
3. Display commission. cout << “commission is: “ << commission << endl ;
if (sales>=0&&sales<=20000)
commission = (sales*0.05);
else if (sales>=20001 && sales <=50000)
commission= (sales*0.07)+1000;
else if (sales>=50001)
commission=(sales*0.1)+3100;
else
cout << "error";
}
15. In this exercise, you will create a program that displays the number of daily calories needed to
maintain your current weight. The number of calories is based on your gender, activity level, and
weight. The formulas for calculating the daily calories are shown in figure 6-44.
Moderately active female : total daily calories = weight multiplied by 12 calories per pound
Relatively inactive female: total daily calories = weight multiplied by 10 calories per pound
Moderately active male: total daily calories = weight multiplied by 15 calories per pound
Relatively inactive male: total daily calories = weight multiplied by 13 calories per pound
Gender Activity Weight
F I 150
F A 120
M I 180
M A 200
a. Create an IPO chart for the problem, and then desk-check the algorithm using the test data
included in figure 6-44. Also desk-check it using invalid data, such as X as the gender code, K as
the activity code, or a negative number for the weight.
b. List the input, processing, and output items, as well as the algorithm, in a chart similar to the one
shown earlier in figure 6-27. Then code the algorithm into a program
c. Desk-check the program using the same data used to desk-check the algorithm.
if (gender=='F'||gender=='f')
{
if (activity=='M'||activity=='m')
calories = weight *12 ;
else if (activity=='R'||activity=='r')
calories = weight *10;
}
else if (gender=='M'||gender=='m')
{
if (activity=='M'||activity=='m')
calories = weight *15 ;
else if (activity=='R'||activity=='r')
calories = weight *13;
}
cout <<"Your total daily calories is: " <<calories << endl;
return 0;
}
16. In this exercise, you will create a program that displays both the smallest and largest of three
integers entered by the user. For example, if the user enters the numbers 3, 5, and 9, the
program should display the messages “Smallest number is 3”. And “largest number is 9” on the
computer screen.
a. Create an IPO chart for the problem, and then desk-check the algorithm four times. For the
first desk-check, use the number 3, 5, and 9. For the second desk-check , use 7, 10, and 12.
For the third desk-check use 8,4, and 6. For the fourth desk-check , use 1, 9 and 1.
b. List the input, processing and output items, as well as the algorithm, in a chart similar to the
one shown earlier in figure 6-27. Then code the algorithm into a program.
c. Desk-check the program using the same data used to desk-check the algorithm.
{
if (a<b&&a<c)
smallest=a;
else if (b<a&b<c)
smallest=b;
else if (c<a&&c<b)
smallest = c;
}
{
if (a>b&&a>c)
largest=a;
else if (b>a&&b>c)
largest = b;
else if (c>a&&c>b)
largest = c;
}
cout<< largest << " is the largest" << endl;
cout << smallest <<" is the smallest" << endl;
return 0;
}