0% found this document useful (0 votes)
13 views3 pages

Tutorial 4 - Part A - Selection Control Structure

The document outlines a tutorial for BACS1013 & BACS1014 focusing on selection control structures in programming. It includes various exercises such as writing if statements, flowcharts for salary raises, and converting if-else statements to switch statements. Additionally, it addresses code inefficiencies and runtime errors, providing practical coding tasks for students to complete.

Uploaded by

thuj-wp24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

Tutorial 4 - Part A - Selection Control Structure

The document outlines a tutorial for BACS1013 & BACS1014 focusing on selection control structures in programming. It includes various exercises such as writing if statements, flowcharts for salary raises, and converting if-else statements to switch statements. Additionally, it addresses code inefficiencies and runtime errors, providing practical coding tasks for students to complete.

Uploaded by

thuj-wp24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

BACS1013 & BACS1014 Problem Solving and Programming Tutorial 4 (Part A)

Tutorial 4 - Part A - Selection Control Structure


1. Suppose we take input x as an integer.
int x;
cin >> x;
Write the if statement that runs under the specified condition below. The first one is done for
you as an example.
(a) x is a positive number
if ( x > 0 )
(b) x is between 3 and 30, including 3 and 30
(c) x is an even number, not including zero
(d) x is a number that ends with zero, such as 420

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)

5. Convert the following if/else sequence to a switch statement.


int x;
cin >> x;
if (x == 2)
cout << "x=2";
else if (x==3 || x==5) {
cout << "x=3 or 5";
x++;
}
else if (x==4)
cout << "x=4";
else
cout << "Else.";

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;
}

7. Rewrite the following multi-way if-else statement by using a switch statement:


if (character == 'A')
cout << "Hello!";
else if (character == 'B')
cout << "Happy Birthday!";
else if (character == 'C')
cout << "Thank you!";
else
cout << "Welcome!";

8. The code below can be compiled, but it contains run-time errors.


int x;
cout << "Enter a number: ";
cin >> x;
switch(x) {
case 1:
cout << "You typed 1.\n";
case 2:
cout << "You typed 2.\n";
default:
cout << "You did not type 1 or 2.\n";
}
(a) What is the error in the code above?
(b) What is the output of the code if the user types in 1?
(c) Demonstrate how the code above can be improved.

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:

Year Code Fee (RM)


1 1200.45
2 or 3 A 1789.00
B 2005.50
Others 2200.90
4 2267.30

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?

int teamWins, teamLosses;

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

You might also like