DCP5101 Lab 6
DCP5101 Lab 6
NOTE:
LAB OBJECTIVES
At the end of this lab activity, the students should be able to:
● Create a complete user-defined function (function prototype, function call and function
definition)
● Pass values from one function to another function
● Use any built-in function if necessary.
2020 1
DCP5101 PROGRAM DESIGN LAB 6
QUESTION 1
● In main():
▪ Display the menu.
▪ Call function get_choice() to get user’s input for mathematical operation to perform.
▪ Prompt the user for two integer numbers.
▪ Call function calculate(…) and pass the numbers and the choice.
● In get_choice():
▪ Ask the user to enter his/her choice.
▪ Use a loop to make sure the user enters the correct value for choice (while the
entry/choice is wrong, ask the user to re-enter the value).
▪ Return the choice to main().
● In calculate(…):
▪ Use a switch-case statement to perform calculation on the two numbers based on
the operation input by user.
Operation A :
answer = number 1 + number 2
Operation B :
answer = number 1 * number 2
Operation C :
answer = number 1 - number 2
Operation D :
answer = number 1 % number 2
Operation E:
answer = 𝑛𝑢𝑚𝑏𝑒𝑟 1 𝑛𝑢𝑚𝑏𝑒𝑟 2 [use appropriate build-in function]
2020 2
DCP5101 PROGRAM DESIGN LAB 6
QUESTION 2
Write a program to convert centimeter (cm) to inches. Don’t forget to write the
function prototype
● In main():
▪ Call function get_input().
▪ Ask the user whether he/she wants to continue or not. repeat
▪ If yes, continue the process using a loop.
● In get_input():
▪ Prompt the user to enter a value in centimeter.
▪ Then call function cm_to_inches(…) and pass the centimeter.
● In cm_to_inches(…):
▪ Convert the value in centimeter to inches:
1 cm is 0.394 inches. Set this value as a constant using preprocessor directive.
▪ Display the output as shown in the sample output.
SAMPLE OUTPUT:
2020 3
DCP5101 PROGRAM DESIGN LAB 6
QUESTION 3
Write a program to calculate total amount of fees need to be paid for subjects’ registration.
In main():
Prompt the user to enter number of subject to register.
Maximum subjects to register is 3. While/if the user input more than 3:
o Display the appropriate message
while o and prompt the user again to key in the correct value. (use while loop)
● Repeat the following process using for loop based on the number of subjects that the
user entered earlier:
o Ask the user to enter the subject code.
o Call function get_credit_hour(…) to get the credit hour and pass the subject
code.
for o Calculate the fee based on the cost per credit hour (RM150) and the number of
credit hours for one subject (returned by get_credit_hour(…) function).
● In function get_credit_hour(…):
▪ Use if statement to identify the credit hours for each subject code. Use built-in function
strcmp().
● In function display_records(…):
▪ Display the subject code, credit hour and fee.
2020 4
DCP5101 PROGRAM DESIGN LAB 6
2020 5
DCP5101 PROGRAM DESIGN LAB 6
QUESTION 4
● In main():
▪ Prompt the user to input hour and minute entered into the parking lot and hour and
minute left from the parking lot. User must use 24-hour format to key in the input.
▪ Call function calculateCharges(…) and send hour and minute entered and hour and
minute left as argument.
▪ Call function display(…) and send the total charges as argument.
● In calculateCharges(…):
▪ Display number of hours and minutes the user parks his/her car.
▪ The parking place charge RM1.50 for the first three hours. If a vehicle parked more than
3 hours, the place charges an additional RM1.00 per hour for each extra minutes or hour.
▪ Return total parking charges to main().
● In display(…):
▪ Display the total charges.
SAMPLE OUTPUT 1:
SAMPLE OUTPUT 2:
SAMPLE OUTPUT 3:
2020 6