Você está na página 1de 32

Lab 5

SELECTION STRUCTURE
(Part I)
Learning Outcomes
After completing this lab, you will be able to:
o use Boolean expressions
o use the relational and logical operators
o know the structure of one-way and two-way selection
o implement decisions using one-way and two-way selection

A. PRE-LAB ACTIVITIES
Question 1
Name TWO (2) values for Boolean expressions?

Answer

Question 2
Who is the inventor of Boolean algebra?

Answer

Question 3
Define the relational operator.

Answer
Question 4
List SIX (6) C++ symbols for relational operators.

Answer

Question 5
What is the difference between operators ‘=’ and ‘==’?

Answer

Question 6
Give the meaning of the following simple Boolean expressions:

For example: 5 < 10  5 is less than 10

Answer

Boolean Expression Meaning


9 < 16

8 != 8

3.5 > 6.8

6.9 <= 8.5

5 == 5

Question 7
Give the value TRUE or FALSE for the following simple Boolean expressions:

Assume that i = 4 and j = 6.


Answer

Boolean Expression Value (TRUE or FALSE)


2 == 3

i == 4

j >= 5

j != i

i < j + 1

Question 8
What is logical operator?

Answer

Question 9
List THREE (3) C++ symbols for logical operators.

Answer

Question 10
What is the difference between operator ‘&&’ and ‘||’?

Answer

Question 11
Give the value TRUE or FALSE for the following compound Boolean expressions (NOT):

Answer

A !A
TRUE
FALSE
Question 12
Give the value TRUE or FALSE for the following compound Boolean expressions (AND,
OR):

Answer:
A B A && B A || B
TRUE TRUE
TRUE FALSE
FALSE TRUE
FALSE FALSE

Question 13
Give the value TRUE or FALSE for the following compound Boolean expression:

Assume that x = 5, y = 6 and z = 7

Answer

BOOLEAN EXPRESSION VALUE (TRUE or FALSE)


x < y && z > y

x <= z && x > 0

x >= y && z == 3

y < 15 || !y > 0

x > y || z > y && !z <= 0

Question 14:
Write the syntax statements for the following flow chart.

true
condition statement 1

false
Answer

Question 15
Write the program statements for the following flow chart.

true
Price ≥ 50? Discount = 0.2

false

Answer

Question 16
What is compound statement?

Answer
Question 17
What is the symbol used for compound statements?

Answer

Question 18
Write the program statements for the following flow chart.

true
Price ≥ 50? Discount = 0.2

false
NewPrice = Price – (Price *Discount)

Answer

Question 19
What is the purpose of two-way selection?

Answer
Question 20
Draw a flow chart for a two-way selection form structure.

Answer

Question 21
Write the syntax statement of general form for two-way selection structure.

Answer

Question 22
Rewrite the following codes into two-way selection structure.

if (mark >= 50)


cout << “PASSED” << endl;
if (mark < 50)
cout << “FAILED” << endl;

Answer
Question 23
Rewrite the following codes into two-way selection structure.

if (price >= 2000)


interest = 3.5;
if (mark < 2000)
interest = 2.5;

Answer

Question 24
Rewrite the following codes into two-way selection structure.

if (income <= 1000 )


tax = income * 0.0125;
if (income > 1000)
tax = income * 0.0225;
incomeAfterTax = income – tax;

Answer
Question 25
Rewrite the following codes into two-way selection structure.

if (year > 5 )
{
dividend = investment * 6.5;
balance = investment + dividend;
}

if (year <= 5)
{
dividend = investment * 3.5;
balance = investment + dividend;
}
cout << “The balance in your account is ” << balance << endl;

Answer

Question 26
Write the program statements based on the flow chart.

false true
Display Number can be
divided by 2? Display
“odd number” “even number”
Answer

Question 27
Write the program statements based on the flow chart.

false true
Discount =2% x Sale Sale >= 5000? Discount = 5% x Sale

Sale after Discount Sale after Discount

= Sale - Discount = Sale - Discount

Display

Sale after Discount

Answer
Question 28
Write the following sentences to program statements.

If the payment is between RM5,000 to RM10,000, displays a message that the customer
will be given a free air ticket to Langkawi. Otherwise displays a message that the
customer will only be given a free lunch in Quality Hotel.

Answer
B. LAB ACTIVITIES
Exercise 1

Type the following program statements. Save your program as lab5_prog1.cpp.

//Program Name: lab5_prog1.cpp


//This program displays a status based on the mark.
#include <iostream.h>

main()
{
int mark;
cout << “Enter the mark : ”;
cin >> mark;
if (mark >= 50)
cout << “PASSED” << endl;
}

Program 5.1

a. Compile and run the program. What is the program output if the mark is 60?

a. Run the program again. What is the program output if the mark is 50?

b. Run the program again. What is the program output if the mark is 45?
Exercise 2

Edit your program according to the following program statements. Save your program
as lab5_prog2.cpp.

//Program Name: lab5_prog2.cpp


//This program displays messages based on the mark range.
#include <iostream.h>

main()
{
int mark;
cout << “Enter the mark : ”;
cin >> mark;
if (mark >= 50)
cout << “PASSED” << endl;
cout << “Thank you for using this program.” << endl;
}
Program 5.2

a. Compile and run the program. What is the program output if the mark is 50?

b. Run the program again. What is the program output if the mark is 45?

Exercise 3

Edit your program according to the following program statements. Save your program
as lab5_prog3.cpp.

//Program Name: lab5_prog3.cpp


//This program displays messages based on the mark.
#include <iostream.h>

main()
{
int mark;
cout << “Enter the mark : ”;
cin >> mark;
if (mark >= 50)
{
cout << “PASSED” << endl;
cout << “Thank you for using this program.” << endl;
}
}
Program 5.3

a. Compile and run the program. What is the program output if the mark is 50?

b. Run the program again. What is the program output if the mark is 45?

c. Compare Program 5.2 and Program 5.3. What is the difference between these
two programs?

Exercise 4

Edit your program according to the following program statements. Save your program
as lab5_prog4.cpp.

//Program Name: lab5_prog4.cpp


//This program displays messages based on the mark.
#include <iostream.h>

main()
{
int mark;
cout << “Enter the mark : ”;
cin >> mark;
if (mark < 50)
{
cout << “FAIL” << endl;
cout << “You have to repeat this subject.” << endl;
}
cout << “Thank you for using this program.” << endl;
}

Program 5.4
a. Compile and run the program. What is the mark for the program to display the
following messages?

FAIL
You have to repeat this subject.
Thank you for using this program.

b. Run the program again. What is the mark for the program to display only the
following message?

Thank you for using this program.

Exercise 5

Type the following program statements. Save your program as lab5_prog5.cpp.

//Program Name: lab5_prog5.cpp


//This program is to display the lower value between 2 numbers
#include <iostream.h>

main()
{
int no1, no2, min;
cout << “Enter the first value : ”;
cin >> no1;
cout << “Enter the second value : ”;
cin >> no2;
if (no1 < no2)
min = no1;
if (no2 <= no1)
min = no2;
cout << “The lower value is ” << min << endl;
}

Program 5.5

a. Compile and run the program. What is the output if the value for no1 is 5 and
no2 is 6?
b. Edit the program to detect a maximum value?

Exercise 6

Type the following program statements. Save your program as lab5_prog6.cpp.

//Program Name: lab5_prog6.cpp


//This program is to identify whether a customer can apply for
//credit card or not
#include <iostream.h>

main()
{
float basicSalary;
const float LIMIT = 1500;
cout << “Enter your basic salary in RM : ”;
cin >> basicSalary;
if (basicSalary >= LIMIT)
cout << “You are able to apply for credit card.” << endl;
}

Program 5.6

a. Compile and run the program. What is the value of basic salary that allows a
customer to apply for a credit card?

a. Edit the program to display the following messages if the basic salary is more
than or equal to 1500:

Congratulations!
You are able to apply for credit card.
Please contact our nearest branch for application.
Thank you for using this program.

Otherwise only the following statement will be displayed.

Thank you for using this program.

Exercise 7

Type a program based on the following flow chart. Your program is able to read price
from the keyboard. Save your program as lab5_prog7.cpp.

true
Discount = 5 % x Price
Price ≥ 1000?

false
PriceAfterDiscount = Price – Discount

Display

“The price after discount is….”

Display

“Thank you”
Exercise 8

Type the following program statements. Save your program as lab5_prog8.cpp.

//Program Name: lab5_prog8.cpp


//This program displays a message based on the water bill amount.
#include <iostream.h>

main()
{
float bill;
cout << “Enter the water bill amount : ”;
cin >> bill;
if (bill >= 350)
cout << “You will receive a 1.5% discount.” << endl;
else
cout << “You will receive a 0.5% discount.” << endl;
}

Program 5.8

a. Compile and run the program. What is the program output if the bill is 350?

b. Run the program again. What is the program output if the bill is 349?
Exercise 9

Edit your program according to the following program statements. Save your program
as lab5_prog9.cpp.

//Program Name: lab5_prog9.cpp


//This program displays a message based on the water bill amount.
#include <iostream.h>

main()
{
float bill, discount, payment;
cout << “Enter the water bill amount : ”;
cin >> bill;
if (bill >= 500 && bill < 1000)
{
cout << “You will receive a 1.5% discount.” << endl;
discount = bill * 1.5;
}
else
{
cout << “You will receive a 0.5% discount.” << endl;
discount = bill * 0.5;
}
payment = bill – discount;
cout << “You have to pay only RM ” << payment << endl;
}

Program 5.9

a. Compile and run the program. What is the program output if the bill is 400?

b. Run the program again. What is the bill amount if the customer wants to have
1.5% discount?
Exercise 10

Below is an executable program by using one-way selection structure.

//Program Name: lab5_prog10.cpp


//This program is to display the lower value
#include <iostream.h>

main()
{
int no1, no2, min;
cout << “Enter the first value : ”;
cin >> no1;
cout << “Enter the second value : ”;
cin >> no2;
if (no1 < no2)
min = no1;
if (no2 <= no1)
min = no2;
cout << “The lower value is ” << min << endl;
}

Program 5.10

a. Rewrite the program by modifying the program into two-way selection. Save
your program as lab5_prog10.cpp.

b. What is the output if the value for no1 is 5 and no2 is 6?


Exercise 11

Type a program based on the following flow chart. Your program should be able to read
price from the keyboard. Save your program as lab5_prog12.cpp.

Charge = 5 % x Price false true Charge = 3 % x Price


Price ≥ 1000?

Price after Charge = Price + Charge Price after Charge = Price + Charge

Display

“The price after charge is….”


Exercise 12

You are given the following requirements:

a. Your program is able to read two inputs from the keyboard, which are the amount
of investment and profit.

b. If the investment is more than or equal to RM50,000 and the profit is more than
RM20,000, then the following message is displayed:

This investment is worthy to continue.

c. Otherwise displays the following message

This investment is a waste of money.

Write a program based on the above requirements. Save your program as


lab5_prog13.cpp.
Exercise 13

You are given the following requirements:

a. Your program is able to read the amount of sale for a sale executive.

b. If the sale is more than RM10,000, then the commission will be 5% of the sale.
Otherwise the commission is only 3% of the sale.

c. Your program is able to calculate the amount of commission by multiply the


percentage of commission with sale.

d. Your program is also able to display the amount of commission to the sale
executive.

Write a program based on the above requirements. Save your program as


lab5_prog14.cpp.
Name :

Student ID :

Date :

C. POST-LAB ACTIVITIES
Question 1

YDL Nation Bank wants to develop a program that can help the customer to calculate the
monthly installment. The program should be able to receive two inputs from the customer,
which are the amount of loan in Ringgit Malaysia and year of installment. Based on these two
inputs, the program should therefore be able to calculate the monthly installment. The table for
the interest calculation is provided as follows:

Year Interest (%)


<5 4.5
≥5 6.5

The formulas for the monthly installment calculation are as follows:

Interest (RM) = Interest (%) x Amount of Loan


Total Loan with Interest = Amount of Loan + Interest (RM)
Monthly Installment = Total Loan with Interest / (Year of Installment x 12)
Name :

Student ID :

Date :
Name :

Student ID :

Date :

Question 2

Nova Hypermarket in your town wants to organize an annual sale. The owner of the
hypermarket wants you to help him to develop a program to help the cashier to calculate and
display the payment after discount. The table for the discount is provided as follows:

Amount (RM) Discount (%)


≥ 300 5
< 300 No Discount

The formulas for the calculation are as follows:

Discount (RM) = Discount (%) x Amount (RM)


Payment After Discount = Amount (RM) – Discount (RM)
Name :

Student ID :

Date :
Name :

Student ID :

Date :

Question 3

SSY Co Ltd is one of the finance companies. This company wants you to develop a program to
help up in calculating the bonus for its employees. The bonus will be calculated based on the
performance mark of the employee. Your program should be able to receive performance mark
from the user and display the bonus to the user as well. The table for the bonus is provided as
follows:

Performance Mark Bonus (RM)


6 ≤ Mark ≤ 10 2000
Mark < 6 1000
Name :

Student ID :

Date :

Question 4

YDL Nation Bank wants to do some modification to their program so that it can help the
customer to calculate the monthly installment by using new condition. The program however is
still able to receive two inputs from the customer, which are the amount of loan in Ringgit
Malaysia and year of installment. Based on these two inputs, the program is able to calculate
the monthly installment. The table for the interest calculation is provided as follows:

Condition Interest (%)


5 ≤ Year ≤ 10 6.7
3 ≤ Year < 5 5.5

The formulas for the monthly installment calculation are as follows:

Interest (RM) = Interest (%) x Amount of Loan


Total Loan with Interest = Amount of Loan + Interest (RM)
Monthly Installment = Total Loan with Interest / (Year of Installment x 12)
Name :

Student ID :

Date :

Question 5

Nova Hypermarket in your town wants to organize another annual sale. The owner of the
hypermarket wants you to help him to modify the program to help the cashier to calculate and
display the payment after discount based on new conditions The table for the discount is
provided as follows:

Amount (RM) Discount (%)


300 ≥ Amount ≥ 1000 5.5
Amount < 1000 6.5

The formulas for the calculation are as follows:

Discount (RM) = Discount (%) x Amount (RM)


Payment After Discount = Amount (RM) – Discount (RM)
Name :

Student ID :

Date :

Question 6

SSY Co Ltd is one of the finance companies. This company wants you to develop a program to
help up in calculating the bonus for its employees. The bonus calculated based on the sales of
the employee. Your program is able to receive sales in Ringgit Malaysia from the user and
display the bonus to the user. The table for the bonus is provided as follows:

Sales (RM) Bonus (RM)


50,000 ≤ Sales ≤ 100,000 5000
35,000 ≤ Sales < 50,000 3000
Name :

Student ID :

Date :

Você também pode gostar