Practical 7 (Part A) - User-Defined Function I
Practical 7 (Part A) - User-Defined Function I
Practical 7 (Part A)
(i) Create a function named display_menu to display the menu as shown above.
(ii) Create three different functions that perform the correspondent arithmetic calculation:
● Function square will read a number from the user, calculate the square of the
number (number2), and print the answer on the screen.
Page 1 of 5
BACS1013 & BACS1014 Problem Solving and ProgrammingPractical 7 (Part A)
● Function squareroot will read a number from the user, calculate the square root
of the number (√number), and print the answer on the screen.
● Function power will read two numbers, x and y, from the user. Then it will calculate
and display the answer of x power of y (xy).
(iii) If the user presses E. The program should print “Program ends now” and stops.
(iv) Create a function named valid_input to return the valid character entered by the user.
The function will perform the following:
● Accept only a character from the user.
● Perform validation using appropriate functions (e.g. isdigit). For instance, if the
user enters a digit, display “Digit is not allowed”.
● If the user enters lower case character (e.g. ‘a’), it will convert the input to upper case
(e.g. ‘A’).
● If the user enters character other than S, Q, P or E, it displays “Please enter only S, Q,
P or E”.
● Displays appropriate message and prompts for input again until the user enters the
desired option.
(v) Lastly, complete the main program to produce the solution for the problem above. Use while
loop to repeat the program above until the user chooses the option of ‘E’.
Pass-by-value Functions
3. Write a program to ask the user to enter a single character (e.g. “*”) and then write the
printOutput method to accept the character and display the output as follows:
*****
****
***
**
*
Note: Use nested loop to produce the output above
that computes the sum of all the positive integers from 1 to n (inclusive). In your program, include
a main function to test the function that you have written. The main function should prompt the
user to enter an integer and then display the sum of all the positive integers up to that integer
value.
Page 2 of 5
BACS1013 & BACS1014 Problem Solving and ProgrammingPractical 7 (Part A)
5. Write a C++ program that will calculate the perimeter and area of a right triangle. You must use
three functions:
(i) Function calculatePerimeter to calculate the perimeter of the triangle and
returns the perimeter value.
(ii) Function calculateArea that will calculate the area of the triangle and
returns the area value.
(iii) Function displayResult that will display the perimeter and area values on
the screen.
Your main program will ask the user to provide the length of two sides (x and y) of the right
triangle. Use cmath library function to assist you. Given below are the formulas:
z 2 = x2 + y2
1 z
area = x y x
2
6. Write a function Get_grade that takes a student’s mark to determine the grade based on the
table below, and returns the grade as a letter character. Write a function to validate the user’s
input to verify that it is within 0 to 100 marks.
80-100 A
65-79 B
50-64 C
40-49 D
0-39 F
Write a function Get_grade_point that takes the student's letter grade (i.e. A,B,C,D,F) as a
char and returns the corresponding numerical grade point (4.0,3.0,2.0,1.0,0.0).
cout << "The corresponding grade is: " << Get_grade(mark) << endl;
cout << "The corresponding Grade Point is: "
<< Get_grade_point(grade) << endl;
Page 3 of 5
BACS1013 & BACS1014 Problem Solving and ProgrammingPractical 7 (Part A)
Extra exercise
1. Write a C++ program with function that will calculate the sum of the following series:
1 2 2 33 4 4 5 5
+ + + +
1! 2! 3! 4! 5!
2. Write a C++ program that will create a menu as below. Each option Add, Subtract, Divide, Multiply
must be processed accurately with function. Requirements of the program:
a. This program only will exit when user key-in Q selection.
b. When chosen selection has been performed, it must go back to the menu selection.
c. Selection is non-case sensitive. E.g A = a, S = s, …
d. Remark: Remember to validate the input, and prompt appropriate error message. Error
by the user may include non-digit character, and divide a number with zero.
3. Easy Car Park charges fees according to vehicle type and amount of time parked:
(a) Write a function void displayVehicleRate that accepts the vehicle type as parameter, then
displays (in one line) the vehicle name in full and the corresponding parking rate as listed
above.
(b) Write a function char getVehicleType that will prompt a user to enter a vehicle type and
return the validated vehicle type.
(c) Write a function int calcHoursParked (int inHr, int inMin, int outHr, int outMin) that
calculates and returns the hours parked as an integer. If any data or result is abnormal, it
returns -1. Eg. if a vehicle entered at 10:30 and exited at 14:20 (international time), the 4
parameters would be 10, 30, 14, 20 and the function should return the value 4 (any fraction
of an hour will be treated as 1 hour). Vehicles cannot enter / exit after midnight and before
7:00am, nor are they allowed to park overnight.
(d) Write a function parkingCharge that receives 2 parameters: vehicleType and hrsParked, and
returns the appropriate parking charge.
(e) Write the main program that calls the above functions as appropriate and displays the parking
ticket. (If the hours parked was returned as -1, display an appropriate message, eg. “Error,
please see the management.”) Eg of format:
Page 4 of 5
BACS1013 & BACS1014 Problem Solving and ProgrammingPractical 7 (Part A)
Page 5 of 5