0% found this document useful (0 votes)
17 views6 pages

DCP5101 Lab 6

Uploaded by

iloveulol428
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)
17 views6 pages

DCP5101 Lab 6

Uploaded by

iloveulol428
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/ 6

DCP5101 PROGRAM DESIGN LAB 6

NOTE:

- Create a folder on Desktop to save your works.


Name the folder appropriately (e.g. Lab 2)
REMINDER!
- Use comment // to write your name, ID, Group Save your
and Lab Question in each program. program as C
language, not
- Save your file as .c C++ (cpp).

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

Write a program to perform mathematical operation based on user’s choice.

● 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]

▪ Display the answer.

SAMPLE OUTPUT 1: User chose E SAMPLE OUTPUT 2: Wrong code


A. Add numbers A. Add numbers
B. Multiply numbers B. Multiply numbers
C. Subtract numbers C. Subtract numbers
D. Remainder of numbers D. Remainder of numbers
E. Power of numbers E. Power of numbers
What is your choice? : E What is your choice? : c
Enter 2 numbers : 2 3
Answer : 8 Your choice is invalid. What is your choice? : d

Your choice is invalid. What is your choice? : D


Enter 2 numbers : 11 3
Answer : 2

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).

fee for one subject = RM 150 X subject credit hours

o Calculate total fee for all the subjects registered.


o Then call function display_records(…) and pass the subject code, credit hour and
fee amount for one subject.
▪ Display the total fees to be paid.

● In function get_credit_hour(…):
▪ Use if statement to identify the credit hours for each subject code. Use built-in function
strcmp().

Subject Code Credit hour


DCS5038 4
DET5078 3
DPR5038 2

▪ Return the credit hour to main().

● In function display_records(…):
▪ Display the subject code, credit hour and fee.

2020 4
DCP5101 PROGRAM DESIGN LAB 6

SAMPLE OUTPUT 1: More than 3 subjects

SAMPLE OUTPUT 2: 3 subjects

SAMPLE OUTPUT 3: 1 subject only

2020 5
DCP5101 PROGRAM DESIGN LAB 6

QUESTION 4

Write a program to calculate parking charges.

● 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

You might also like