0% found this document useful (0 votes)
29 views

Practical 7 (Part A) - User-Defined Function I

This document provides instructions for 6 programming problems involving functions in C++. Problem 1 involves writing functions to display a banner and menu. Problem 2 involves writing functions to calculate square, square root, and power based on user input. Problem 3 involves a function that prints a pattern based on user input. Problem 4 involves a function that calculates the sum of integers from 1 to a user-input number. Problem 5 involves functions to calculate the perimeter and area of a right triangle. Problem 6 involves functions to determine a student's grade and grade point based on their marks. There are also extra problems provided as optional exercises.

Uploaded by

yang ma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Practical 7 (Part A) - User-Defined Function I

This document provides instructions for 6 programming problems involving functions in C++. Problem 1 involves writing functions to display a banner and menu. Problem 2 involves writing functions to calculate square, square root, and power based on user input. Problem 3 involves a function that prints a pattern based on user input. Problem 4 involves a function that calculates the sum of integers from 1 to a user-input number. Problem 5 involves functions to calculate the perimeter and area of a right triangle. Problem 6 involves functions to determine a student's grade and grade point based on their marks. There are also extra problems provided as optional exercises.

Uploaded by

yang ma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

BACS1013 & BACS1014 Problem Solving and ProgrammingPractical 7 (Part A)

Practical 7 (Part A)

Value returning Functions

1. Write the required functions as below:


a. Write a function called MusicBanner to print the banner:
**********************************
* Welcome to *
* ABC Music School *
**********************************

b. Write a function called MusicMenu to print a menu as follows:


ABC Music School
1. Piano
2. Violin
3. Guitar

c. In the main program, perform the following:


● Call the MusicBanner and MusicMenu to display the output.
● Allow the user to key in the selection. Your program should display the correspondent
message to the user’s selection. For instance, if user chooses 1 then the program
would display
Your choice is Piano.
● Display the fees for the type of courses chosen. Fees per month for piano, violin and
guitar are RM120, RM150 and RM80 respectively. For instance, if the user chooses 2,
then the program would display
Piano class is RM120 per month.

2. Write a C++ program that is able to perform the following:


SELECT OPTION TO CALCULATE:
==========================
[S] square of a number
[Q] squareroot of a number
[P] X power of Y
Press [E] to exit the program

Enter option >>

(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

4. Write a C++ program that contains the following function:


int sumToN (int n)

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).

Write the main program that includes the statements as follows.


int mark;
char grade;

cout << "Enter your marks: ";


cin >> mark;

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:

Vehicle Vehicle Type Rate


Car c RM 2 per hour, or part thereof
Bus b RM 3 per hour, or part thereof
Truck t RM 4 per hour, or part thereof

(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

You might also like