The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.
Discover the best professional documents and content resources in AnyFlip Document Base.
Search
Published by Wan Anisha, 2022-04-10 10:17:12

C++ eBook

C++ eBook 2022

LAB EXERCISES

Lab #6 (FUNCTIONS)

1. The following table shows the rate of basic salary, EPF and SOCSO according to thescheme of the
employee.

a)Write the definition of a function named CalcEPF() that receives the scheme and basic LAB EXERCISES
salary. The function will calculate the EPF using the rate shown in the table above and return
the result.

b)Write the definition of a function named Calcsocso() that receives the scheme and basic
salary. The function will calculate the SOCSO using the rate shown in the table above and
return the result.

c) Write a main program that requires the user to enter the employee's name, employee's
scheme and basic salary. The program will call the above functions to calculate the EPF and
SOCSO. The program will also calculate the net pay for the employee. Print the entire
employee's information including EPF, SOCSO and net pay.

2. Write a program that asks the user to enter an integer number. Create a function Identify()
which received the number and return the power and square root of the number if the number
is a natural number. Otherwise, display “NEGATIVE NUMBER”. Stop the program when the user
enter -1.

3. As a programmer in CDNet Sdn. Bhd, you have been asked to develop a home loan calculator
using C++ programming language. In this calculator, the customer needs to input name, address,
loan amount, number of years for loan and yearly interest rate. Given below

a) Write a function CalcHLoan() that calculates home loan instalment. It will receive loan
amount, interest rate and years in month. Then it will return the instalment.

b) Write a function TotalInt() that calculates the total interest customer paid. The function
receives loan amount, interest rate and years and returns the total interest.

c) Write a main program that requires the user to enter the customer's name, address, loan
amount, interest rate and years. The program will calculate the loan instalment and total
interest by using the above functions. Print the entire customer's information including loan
instalment and total interest.

148 | P a g e

4. Distributors for a wellness products get their income based on the following two tables. They LAB EXERCISES
get commission based on the following table:

They also get a bonus based on the following table:

Given the following function prototypes:
float calcTotalSales(int, int, int);
float calcCommission(int, int, int);
float calcBonus(float);
Write the definition for the above functions:
a) Function calcTotalSales() receives the number of bottles for each brand, calculate the

total sales for all brands and returns the result.
b) Function calcCommission() receives the number of bottles for each brand. This function

will calculate and return the commision received.
c) Function calcBonus()receives the total sales and returns the bonus received.

149 | P a g e

LAB EXERCISES

Lab #7 (ARRAY)

1. Create an array to store up to 30 BMI adult. The program to calculate Body Mass Index (BMI)
for each adult by using the following formula: BMI = Weight (kg) / (Height (m) x Height (m))

Display the following message for each of the adult.
• If Your BMI is Less than 18.5, display "You are underweight"
• If Your BMI is 18.5 to less than 25, display " Your weight is desirable"
• If Your BMI is 25 to less than 30, display "You are overweight"
• If Your BMI is 30 or more, display "You are obese"

• Else display invalid data

Create function GetInput(), Identify() and Display() functions for the above program.

2. Create an array to store a series of integer numbers. Create function identify() which accept the
numbers. The function will then calculate the sum and average if the entered integer number is
a positive integer numbers while if the entered integer number is an odd number, the function
will calculate the sum of all the remainders. The program will only stop when the user enters the
sentinel value which is -999.

3. Compass is currently holding a charity car wash for a week. You are asked to write a program
that calculates and displays the total collection, the average collection, and the minimum and
maximum collection for the week. Thus, create an array of floating point of size SEVEN (7) to
störe the total collection per day from Monday through Sunday. Your program should contain
the following functions and main program:

a) Function inputCollection() to prompt the user to input the total collection for each day
and stores it in the array.

b) Function totalCollection() to determine the total collection received by Compass and
returns the result to the main function.

c) Function minCollection() to determine the minimum collection received by Compass and
returns the result to the main function.

d) Function maxCollection() to determine the maximum collection received by Compass
and returns the result to the main function.

e) A main program that uses the above functions and displays the appropriate result.

4. Company ABC is doing an analysis on its 20 salesperson representatives. The analysis is LAB EXERCISES

important for the company to search for the best salesperson whose sale is the highest. That

particular salesperson will be awarded as the token of appreciation from the company. The

salesperson with the lowest sale will be given more training to improve their sales. A computer

program is required to assist the company in doing the analysis. Write a C++ program to do the

following :

a. Declare two arrays namely ID of type int and array sales of type double. Both array’s
size is 20.

b. Prompt the user to enter the salesperson’s id and their respective sales. Then read the
input into the corresponding arrays.

c. Print the salesperson’s id and the amount of sales of the highest and lowest sales.

150 | P a g e

DISCUSSIONS DISCUSSIONS
QUESTION 1
Metro is a new satellite television station that offers various movie packages to its customers. The
table below depicts the details of the movie packages offered for this month:

Write a complete program to do the fol owing tasks:
• Ask the user to enter his/her smart card number, the package code and additional TV series (use
the character codes to input the additional TV series). Display an error message if the user enters
an invalid package code.
• Calculate the total charge before tax for a customer and later, calculate the new charge for each
subscription of the movie package after 6% of the government tax. • Print the receipt using the
following example:

The process will continue until the user enters 'N' to stop. • At the end of the process, print the
following report:

151 | P a g e

DISCUSSIONS DISCUSSIONS

QUESTION 2
The following table shows the average speed per hour for two types of vehicles:

a) Write the definition of a function named calcDistance () that receives the vehicle code
and time travelled in minutes. The function will calculate and return the distance travelled
in kilometer for that type of vehicle. The formula to calculate the distance travelled is as
follows:

Distance Travelled (km) = Average Speed * (Time travelled/60)
b) Write the definition of a function named CalcFuelConsumption() that receives the
distance travelled in kilometer and fuel usage in liter. The function will calculate and
return the fuel consumption in liter per kilometer that has been used by that particular
type of vehicle. The formula to calculate the fuel consumption is as follows:

Fuel Consumption (liter/km) = Fuel Usage / Distance Travelled
c) Write a main program that requires the user to enter the vehicle code, time travelled in
minutes and fuel usage in liter. The program will call all of the functions above to calculate
the distance travelled in kilometer and the fuel consumption in liter per kilometer used by
the vehicle. Later, the program will print all of the information of the vehicle as shown in
the example below:

152 | P a g e

DISCUSSIONS DISCUSSIONS
QUESTION 3
NHAT Hospital needs a program to compute and print a bill Statement for each patient. The
charges for each day are as follows:

Write a program that requires the user to enter the number of days spent in the hospital, the
room type (D, S or B), the telephone Option (Y or N) and the television option (Y or N).
The program will caiculate the patient's bill and print the patient's billing Statement as
example below:
Input: 2 D Y N
where 2 indicates the number of days, D represents the room type, Y represents the
telephone option and N represents the television option.
Output:

153 | P a g e

DISCUSSIONS DISCUSSIONS
QUESTION 4
Cozy Kids Hair is the best salon specialized for kids haircutting from age 1 to 19 years old. Its mission
is to provide "High quality haircut in a fun, safe and comfortable environment". The hair stylists are
specially trained to not only cut.and style kids' hair but to make the haircut a gentle and relaxing
experience.
For this seasons promotion, it has offered interesting packages for kids haircutting as listed in the
table below :

Write a complete C++ program that will accept the menu package as string input, customer's age
and gender ( B-boy or G-girl ) where appropriate. The program should be able to display an error
message "Not in the Fun Menu List" if the data input is incorrect. Based on the values entered:
a) Calculate and display the Net price that each customer has to pay.
b) Count and display how many customers for each fun package.
c) Calculate and display the total price for each fun package.
d) Calculate and display the grand total for all the fun packages.

154 | P a g e

DISCUSSIONS DISCUSSIONS
QUESTION 5
The Paradise Resort has four type of room packages. The rate for each package is stated in
the following table.

Each guest has to pay 5% for the government tax. If the guest uses online booking, the
guest is given 15% discount.
Write a complete C++ program that includes the following statements.
• Read the guest's name, type of package, number of days and type of booking. Use

numeric code for type of package (1 - Apartment, 2 - Family, 3 - Deluxe, 4 -
Standard) and use character code for type of booking (O - Online, M - Manual).
• Calculate the government tax, the total charges for the guest and discount given if
the type of booking is through online.
• Print the receipt for the guest using the following format

• The process will continue until the user request to stop.

155 | P a g e

DISCUSSIONS DISCUSSIONS
QUESTION 6
Distributors for a wellness products get their income based on the following two tables:
They get commission based on the following table:

They also get a bonus based on the following table:

Given the following function prototypes:
float calcTotalSales(int, int, int);
float calcCommission(int, int, int);
float calcBonus(float);
Write the definition for the above functions:
a) Function calcTotalSales() receives the number of bottles for each brand, calculate the
total sales for all brands and returns the result.
b) Function calcCommission() receives the number of bottles for each brand. This
function will calculate and return the commision received.
c) Function calcBonus() receives the total sales and returns the bonus received.
Write a main program which input the number of bottles sold for each brand. Then call all the above
functions.

156 | P a g e


Click to View FlipBook Version