Professional Documents
Culture Documents
Lab 7 C++
Lab 7 C++
Functions
<cstring> for:-
strlen : computes the length of the string, but not including
null characters
strcpy : to copy one string to another
strcmp : takes two strings as arguments and compare these
two strings
Manipulator Manipulator functions are contained in the header file <iomanip>
functions setw():sets the width of the filed assigned for the output.
setprecision():sets the total number of digits to be displayed,
when floating point numbers are printed.
fixed: sets floating-point numbers in a fixed decimal format
1. User-defined functions (Value Returning Functions)
All value returning functions perform a task and then precisely return one value
Example code for value returning functions
Syntax:
Void functions are created and used just like value-returning functions except they do
not return a value after the function executes.
Use the keyword "void."
Example:
Syntax:
Task 1. Trace the output of following code. (Screenshot the output)
a)
b)
c)
d)
e)
f)
g)
h)
i)
j)
k)
l)
b) Determine the output from the program based on the following input.
a. 10 15 20
b. 4 5 3
Task 3.
10 to 19 50.99
20 to 29 45.59
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:
Otherwise:
1. Calculate the profit using the function calcProfit()
2. Display the profit
Task 4.
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) .
Weight (kg)
𝐵𝑀𝐼 =
𝐻𝑒𝑖𝑔ℎ𝑡 ሺ𝑚ሻ𝑥 𝐻𝑒𝑖𝑔ℎ𝑡 (𝑚)
M A
< 40
F B
> 30
M B
> 40
F C
M A
< 40
F A
<= 30
M B
> 40
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()
c. Determine the MP code using the function calcMP()
d. Display the message “Retire at 60” or “Retire at 55” based on the MP code.