Practice with Iterations

Complete these exercises to test your understanding of iterations. 

Question 6a

Write a program named question6a.cpp that will calculate and print pay slips. User inputs are the name of the employee, the number of hours worked and the hourly pay rate. You have to declare three functions. d) one for input; e) one to calculate the employee’s pay; and f) one to print the payslip. The input function has to input the name of the employee, the number of hours worked and the hourly pay rate into the variables theEmployee, theHoursWorked, and thePayRate. The variable employee is a string and the other two variables are of type float. As the values of theEmployee, theHoursWorked, and thePayRate will be changed in this function, reference parameters need to be used. The calculation function will receive two parameters that represent the number of hours worked and the hourly pay rate, do the calculation and return the pay for the employee. An employee, who has worked more than 40 hours, is paid 1.5 times the hourly pay rate for each hour of overtime. As the parameters are not changed in the function, they should be value parameters. The function should return a float value which represents the pay. The output function has to display the name of the employee, the number of hours worked, the number of overtime hours and the hourly pay rate entered by the user as well as the employee’s pay. For example:

Pay slip for Harry Matsipe Hours worked: 43.5 hours Overtime hours: 3.5 Hourly pay rate: R125.35 Pay: R5672.09

The main function includes a for loop that allows the user to repeat the calculation of a pay slip for five employees. We give the main function below. You must submit the three functions you have developed as well as output for repeating the loop five times with the following input data:

Harry Matsipe 43.5 125.35 Ellen Malan 39.4 112.75 Joey Rashdien 40 120.45 Mpho Bopape 41.2 123.60 Veli Singh 39.7 135.30


Question 6b

NuMetro has a special on movies for all members of the public but special discounts for students and pensioners. If pensioners or students buy a movie ticket they receive 10% if they buy a movie and popcorns, they receive 20% discount. 100 Other customers only receive a discount when they buy a movie ticket and popcorn; there is no discount for just a movie ticket alone. Write a program named question6b.cpp that will consist of two functions. The program must prompt the user for type of customer (‘p’ for pensioner, ‘s’ for student, ‘o’ for other). It must then call the relevant function according to that entry. The first function must receive the customer type and calculates discount for pensioners and students. The second function calculates the discount for customers that are not pensioners or students.


Question 6c

Write a program named question6c.cpp that demonstrates the use of the following functions. A C++ function named getName() prompts the user for two string values; first name and last name and return a combination of the two as one value. The second function getHours() calculates employee’s weekly pay, it must receive one argument, fullName, a string variable and a float value for the rate. It must then prompt the user for hours worked for each day of the week, i.e. Monday – Friday and calculates the weekly pay. Employees who have worked more than 40 hours that week will receive a bonus of 10% and those who have worked less than 40 hour will receive 10% less pay for that week.


Source: Wikibooks, https://en.wikibooks.org/wiki/C%2B%2B_Programming/Exercises/Iterations
Creative Commons License This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 License.

Last modified: Monday, August 16, 2021, 3:38 PM