Uploaded by 白言煜

1117A T3 2023

advertisement
Tutorial 3 Worksheet
COMP1117A Computer Programming I 2023-2024
Tutorial objectives:
• To practice the if-else control flow
• To develop computational thinking
• Deadline: 23:59 Oct 5, 2023
Exercise 3.1 Absolute Difference
Write a program to calculate the absolute difference of two input integers.
- Note that the input integers could be zero, negative, or positive.
- Hints: try to convert the input str into int using type casting before the calculation.
- Sample test cases:
Test case ID
Inputs
Outputs
1
0
3
3
2
3
6
-3
3
-2
1
-1
Exercise 3.2 Roots of Quadratic Equations
Write a program to check the number of distinct real roots of a quadratic equation given
its coefficients.
- Suppose the quadratic equation is ax2 + bx + c = 0. The inputs to your program will
be a, b, and c in order. Your program should then print the number of the distinct
real roots of the equation.
- You can assume that input a, b, and c are integers, and a will never be 0.
- Hints: the number of distinct real roots of a quadratic equation can be determined
by its discriminant b2 – 4ac.
- Sample test cases:
Test case ID
Inputs
Outputs
1
1
1
2
1
2
1
2
-3
2
3
1
0
2
4
Exercise 3.3 Body Mass Index
Write a program to calculate the Body Mass Index (BMI) according to the input weight (in
kilograms) and height (in meters) of an adult, and then output the weight status (“Other”,
“Overweight”, or “Obesity”) based on her/his BMI.
- The formula of BMI calculation: BMI = weight / (height)2
- The inputs to your program will be weight and height in order.
- The relationship between BMI and weight status:
BMI
Weight Status
BMI < 25
Other
25 <= BMI < 30
Overweight
30 <= BMI
Obesity
-
Sample test cases
Test case ID
Inputs
1
80
1.8
2
68
1.6
3
100
1.8
Outputs
Other
Overweight
Obesity
-
Please complete these Exercises 3.1 to 3.3 on Moodle.
-
Reminder: You will not get any marks if you didn’t press the “Evaluate” button before
the deadline
Download