Tutorial 4 - Part A - Selection Control Structure
Tutorial 4 - Part A - Selection Control Structure
2. A company has decided to raise the salaries of its employees. The company will determine the
percent rise based on the status and years of service of each employee.
Employee Status Literal Years of Service Percent Raise
Full time ‘F’ Less than 7 years 5.0
Full time ‘F’ 7 years or more 7.0
Part time ‘P’ Less than 7 years 3.5
Part time ‘P’ 7 years or more 4.1
Draw a flowchart to determine the percent rise for an employee based on the table above. Then
write a nested if statement using C++. You must include the variable declarations in your code.
3. Explain why the code below is considered inefficient. Demonstrate how it can be improved.
if (expenses < 100.00)
rebate = 0.2;
if (expenses >= 100.00 && expenses < 500.00)
rebate = 0.4;
if (expenses >= 500.00)
rebate = 0.7;
4. Write the C++ code to represent the selection structure below using if-else statements.
Page 1 of 3
BACS1013 & BACS1014 Problem Solving and Programming Tutorial 4 (Part A)
6. Part of the code segments below contains error(s). Identify the errors.
string s;
getline (cin,s);
switch (s) {
case "hello":
cout << "hi";
break;
default:
cout << "bye";
break;
}
Page 2 of 3
BACS1013 & BACS1014 Problem Solving and Programming Tutorial 4 (Part A)
9. Assume that the tuition fee for year 1 and year 4 is RM1200.45 and RM2267.30 respectively
without considering the Code. The fee for year 2 and year 3 is determined by code as shown in
the table below:
Write a nested switch statement to assign the fee value based on the conditions above.
10. If you execute the following code, what will it display if the user enters 5? 15? 30? -1?
int number;
cout << “Enter a number : ”;
cin >> number;
if (number > 0) {
cout << “Zero ”;
if (number > 10) {
cout << “Ten ”;
if (number > 20) {
cout << “Twenty ”;
}
}
}
11. If you execute the following code, what will it display if the user enters 15 18? 15 10? 9 7?
cout << “Enter the number of team wins and number of team losses : ”;
cin >> teamWins >> teamLosses;
if (teamWins > teamLosses)
{
if (teamWins > 10)
cout << “You are the champion ! \n”;
else
cout << “You have won more than 50% of your games. \n”;
}
else
cout << “Good luck in the rest of your games. ”;
Page 3 of 3