You are on page 1of 3

Lab 10: Functions

Task 1. Given the following C++ code

a) Identify three parameters mark as * for function double calculation ( * , * , *)

b) Determine the output from the program based on the following input.

a. 10 15 20
b. 4 5 3
Task 2.

As a stockiest of a supplement product GELHALAL, Ahmad wants to know the profit of selling this
product as a wholesale. The cost of the supplement product is RM30 per bottle. The following table
shows the selling price of stockiest to dealer.

No of Bottle Price per Bottle (RM)


10 to 19 50.99
20 to 29 45.99
30 and above 40.99

Dealers are NOT allowed to buy less than 10 bottle.

a. Write the definition of function calcProfit (). This function receives the number of bottle
sold to the dealer through its parameter. This function then calculates and returns the profit
using the following formula:

profit = number of bottle x (price per bottle – cost per bottle)

b. Write a main program to do the following:


• Get the input a dealer’s name and the number of bottle
• If the number of bottle is less than 10,
üDisplay the message
“Dealer is not allowed to buy less than 10 bottles!”

üOtherwise:
1. Calculate the profit using the function calcProfit()
2. Display the profit
Task 3.

The government of Malaysia assigned you to write a program to advise all staff to determine what
retirement age to choose based on the criteria given by Body Mass Index (BMI) and Mortality
Predictor (MP) .

BMI is calculated by the following formula:

Weight (kg)
𝐵𝑀𝐼 =
𝐻𝑒𝑖𝑔ℎ𝑡 (𝑚) 𝑥 𝐻𝑒𝑖𝑔ℎ𝑡 (𝑚)

Meanwhile, MP code is determined by the following condition:

BMI Age Gender MP code


> 30 < 40 M A
F B
> 40 M B
F C
<= 30 < 40 M A
F A
> 40 M B
F B

Only those who achieved ‘A’ or ‘B’ rating in their MP will be advised to choose to retire at 60, while
those who achieve ‘C’ must choose to retire at 55.

a. Write the definition of function calcBMI () that receives weight and height. This function
calculates and returns the BMI.
b. Write the definition of function calcMP() that receives BMI, age and gender. This function
determines and returns the MP code
c. Write a main program to do the following:
a. Get the input of weight, height, gender and age.
b. Calculate the BMI using the function calcBMI()//function call
c. Determine the MP code using the function calcMP() //function call
d. Display the message “Retire at 60” or “Retire at 55” based on the MP code.

You might also like