Вы находитесь на странице: 1из 7

EKT 120 - Computer Programming

LAB 4(EXTRA)

CONTROL STRUCTURES : SELECTION

LAB EXERCISE

PART I: SHORT ANSWERED QUESTIONS

1. By using the aid of flowchart(s), explain the difference between if selection and if…
else selection.

2. Write if-else if statements for the following Table 1.

Table 1: Marks and Grade

Marks Grade
80-100 A
70-79 B
60-69 C
40-59 D
20-39 E
0-19 F

3. The following questions are based on the program segment in Figure 3(a) and Figure 3(b):

#include <stdio.h> #include <stdio.h>


int main() int main ()
{ {
float temperature=25.5; float temperature=25.5;
if (temperature >= 33) if (temperature >= 33) if (mark >= 80)
printf(“\n Hot”); printf(“\n Hot”);
else if(temperature >= 25) if(temperature >= 25)
printf(“\n Warm”); printf(“\n
printf
Warm”);
(“\n Grade B ”); printf
else if(mark >= 14) if(markif>=(mark
14) >= 60) if (mark >= 60
printf(“\n Mild ”); printf(“\nprintf
Mild (“\n
”); Grade C ”); printf
else else else else
printf(“\n Cool”); printf(“\nprintf
Cool”);
(“\n Grade D ”); printf
return 0; returnreturn
0; 0; return 0;
} }

Figure 3(a) Figure 3(b)


EKT 120 - Computer Programming
LAB 4(EXTRA)

(i) Predict the output of Figure 3(a) and Figure 3(b).


(ii) Rewrite selection structure in Figure 3(b) to obtain the same result as output in
Figure 3 (a).

4. Identify whether if-else statement in Question (2) can or cannot be converted into switch
statements. Justify your answer.

5. What are differences between switch statement and if-else if statement control
structures? By using the aid of flowchart(s), explain the differences.

6. Write a program segment using switch selection for the following statements.
If the grade is A, print “Your CGPA is 4.0”.
If the grade is B, print “Your CGPA is 3.0”.
If the grade is C, print “Your CGPA is 2.0”.
If the grade is D, print “Your CGPA is 1.0”.

7. Convert the switch case statement below to if… else if statement.


int option;
printf (“Please type 1, 2 or 3\n 1. Breakfast\n 2.Lunch\n
3.Dinner\n”);
scanf (“%d”, &option);
switch (option)
case 1: printf (“Good morning\n Order breakfast\n”); break;
case 2: printf (“Order lunch\n”); break;
case 3: printf (“Order dinner\n”); break;
default: printf (“Order nothing\n”);

8. Predict output of the following program.

#include<stdio.h>

int main()
{
int num=7;

if(num>0)
{
printf(“%d is a positive number\n”,num);

if(num %2 ==0)
printf(“%d is an even number\n”,num);
else
printf(“%d is an odd number\n”,num);

}
else
printf(“%d is a negative number\n”,num);

return 0;
}
EKT 120 - Computer Programming
LAB 4(EXTRA)

PART II: PROBLEM SOLVING QUESTIONS

1. Write a program that calculates the user’s body mass index (BMI) and categorizes it as
underweight, normal, overweight, or obese, based on the following table:

BMI Weight Status


Below 20.9 Underweight
21.0 – 35.9 Normal
36.0 – 45.9 Overweight
46.0 and above Obese

To calculate BMI based on weight in kilograms (kg) and height in meters (m), use this
formula:
weight
BMI 
height  height

2. Write a program to input a number x from the keyboard. If the number is larger than 0,
find its square root. Otherwise, calculate x* x. Write the program by using selection
statement. A sample of the output showed as below.

Please enter a positive or negative number: -4


You have entered -4 which is smaller than 0
Therefore -4 x -4 = -8
Hint: Use include <math.h> to include sqrt function.
Example: root=sqrt(x);

3. A company sells five different products with prices shown in the following table

Product Number Retail Price Unit (RM)


1 15.50
2 19.25
3 14.75
4 25.00
5 12.20

Write a program using switch-case statement that reads the product number and
number of units sold and display the amount to be paid.

Sample of the output shown as below

Please enter a product number: 5


Quantity : 4
Total amount to be paid: RM48.80
EKT 120 - Computer Programming
LAB 4(EXTRA)

4. Create a C program to determine the status and grade of Computer Programming subject based on
midterm examination and final examination. The midterm examination contributes 30% and final
examination contributes 70% of final marks. The program should read input of both examination
marks (full marks is 100 for both examination), calculates and display the final marks. Finally, the
program will display the status and grade as shown in Table 4.1 based on calculated final marks.

Table 4.1: Marks and Grade

Final Marks Status Grade


80-100 A
70-79 B
Pass
60-69 C
40-59 D
20-39 E
Fail
0-19 F

Hint: Use nested if structure in your program.

The sample output should be as follows:

Please enter midterm examination marks: 82


Please enter final examination marks : 75

Final marks : 77.10


Status : Pass
Grade : B
EKT 120 - Computer Programming
LAB 4(EXTRA)

ADDITIONAL TASKS

1. Develop a program for current electrical billing system based on the given requirements
using the if..else statements shown in Table 1.2.

Table 1.2: Electrical Charge Rate


Unit Consumed (k/watt) Charge Rate
0 – 300 RM0.50 per unit
301 and above RM0.65 per unit in excess of 300 units

Sample of the output shown as below

Enter amount of k/watt have been used: 435

Price per k/watt is: 0.50 per unit for 300 units and 0.65 per unit for 135
units
Total amount of bill per k/watt: RM237.75

2. Develop a program to determine the most appropriate transportation option based on


speed needed to travel in designated distance and time entered by users. The formula to
measure speed as follows:
speed=distance / time

The speed and the most appropriate transportation choices is shown in Table 2.2.

Table 2.2: List of transportation choices and their speed range


Speed (km per hour) Transportation Choices
0 - 40 Bicycle
41 – 70 Motorcycle
71-150 Car or Bus
151-300 Supercar
301 and above Aircraft

The output sample for the program should be as follows:

Program to Determine Speed and Transportation Choices


-------------------------------------------------------
Please enter distance to travel (km) : 436
Please enter time to travel (hour) : 5

Speed needed : 87.20 km per hour


Appropriate Transportation Choice : Car or Bus
EKT 120 - Computer Programming
LAB 4(EXTRA)

3. Write a computer program to solve the quadratic equation below:

ax2+ bx + c = 0

The input data is to consist of the values of a, b and c and is to come from the keyboard.
The output is to consist of the values of x and display to the user. However, your computer
must consider all the possibilities, if b2 – 4ac is positive, then equation (1.1 and 1.2) can be
used directly to find the solutions x1 and x2. However, if the b2 – 4ac is negative, the
equation become as in 1.3 and 1.4. Below are the equations.

 b  b 2  4ac
x1  (3.1)
2a

 b  b 2  4ac
x2  (3.2)
2a

b  (b 2  4ac)
x1   (3.3)
2a 2a

b  (b 2  4ac)
x2   (3.4)
2a 2a

Sample of the output show as below:

Enter the values of a, b and c (each separated by a space) then press


enter
15 -2 3
Imaginary result:
x1 = 0.06667 + 0.44222
x2 = 0.06667 – 0.44222

Hint: Use include <math.h> to include sqrt function.


Example: root=sqrt(x);
EKT 120 - Computer Programming
LAB 4(EXTRA)

4. Write a C program to measure perimeter and area of circle and rectangle using switch case
statements. The program should read type of shape (‘C’ for circle or ‘R’ for rectangle) from users.
If users enter ‘C’, prompt users to enter the radius of circle. Otherwise, if users enter ‘R’, prompt
users to enter the width and height of the rectangle. If users enter other than ‘C’ or ‘R’, display
invalid message. Then, determine the perimeter and area of the corresponding shape. The formula
of both shape is shown in Table 4 (Given π=3.142).

Table 4.2: Formula for Perimeter and Area


Shape Perimeter Formula Area Formula
‘C’ for Circle Perimeter=2πj Area=πj2
‘R’ for Rectangle Perimeter=(2 x height)+(2 x width) Area=height x width

The sample output of the program should be as follows:

Program to Measure Perimeter and Area


---------------------------------------
Enter shape : C
Please enter radius of the circle (cm) : 3

Perimeter : 18.85 cm
Area : 28.28 cm2

Program to Measure Perimeter and Area


---------------------------------------
Enter shape : R
Please enter width (cm) : 4
Please enter height(cm) : 7

Perimeter : 22.00 cm
Area : 28.00 cm2

Program to Measure Perimeter and Area


---------------------------------------
Enter shape : T
Invalid shape!

Вам также может понравиться