Lab 07 - Functions
Lab 07 - Functions
Lab 07 - Functions
Lab Exercise 7
Functions – Passing Arguments & Recursive Functions
Due Date: 23 May 2021
Late Submission Policy:
• Up to 4 hours late:10% penalty
• Up to 2 days late: 25% penalty
• More than 2 days late: 50% penalty
• No submission will be accepted after 4 days of the specified deadline: 100% penalty
Part A: Review Questions
1. Write the function prototype for each of the following:
(a) Function hypotenuse that takes two floating-point arguments side1
and side2, and returns a floating-point result.
(b) Function smallest that takes three integers, x, y, z and returns an integer.
(c) Function instructions that does not receive any arguments and does
not return a value.
(d) Function intToFloat that takes an integer argument, number, and
returns a floating point result.
2. Write a C++ statement that assign random integers to the variable n using rand()
function in the following ranges:
(a) 1 ≤ n ≤ 100
(b) 0 ≤ n ≤ 9
(c) 1000 ≤ n ≤ 1112
(d) 1 ≤ n ≤ 1
(e) 3 ≤ n ≤ 11
3. Write the value of x after each of the following statements is executed:
(a) x = fabs(7.5)
(b) x = floor(7.5)
(c) x = fabs(0.0)
(d) x = ceil(0.0)
(e) x = fabs(-6.2)
(f) x = ceil(-6.3)
(g) x = ceil(-fabs(-8+floor(-5.5)))
4. Write the function prototype for the following:
(a) Function sumArray that sums the value of an integer array a[10] and
returns the result as an integer.
1
(b) Function countChar that determines the number of characters in a string.
The string is passed as a pointer to a string *sPtr and the result is return as
an integer.
(c) Function avgValue that calculates the average of a floating-point array of
type double val[5]. The result is passed by reference in the argument of the
function.
(d) Function printString that prints a character string that is passed as a
pointer *stringPtr in the argument of the function.
(e) Function convertTemperature that converts a temperature value in
degree Celsius to Fahrenheit. The temperature values are type double. Use
function call-by-value.
(f) Repeat exercise (e) above but use function call-by-reference.
1. Write a program that plays the game of “guess the number” as follows: Your
program chooses the number to be guessed by selecting an integer at random in
the range 1 to 1000. The program then types: [Hint: use rand() and srand()]
2. Write a program that reads a line of text and prints a table indicating the number
of one-letter words, two-letter words, three-letter words, etc. appearing in the
text. For example, the phrase: “To be or not to be” contains
2
Hint: use built-in functions strtok() and strlen() defined in <cstring>
3. Write a program that takes the following input from the user:
a. Wholesale price of the product
b. Markup percentage
The program should then calculate the retail price of the product by defining
a user-defined function “RetailPrice” which receives the wholesale price
and markup percentage as function arguments and returns the retailprice of
the product.
Sample program run:
4. Write a program that inputs a series of integers and passes them one at a time to
function even which uses the modulus operator to determine if an integer is
even. The function should take an integer argument and return true if the
integer is even and false otherwise.
3
5. Write a function cumulativeSum that sums an integer passed to the function
with the previous accumulated sum value. Use pass-by-reference in the function
argument for the sum variable. An example of program run is shown below.
6. One function takes in a series of double values and then find the largest
number out of the numbers and another function finds the smallest numbers out
of the series of numbers. Write the two functions if:
(a) function largest is called by-reference with pointer argument
(b) function smallest is called by-reference with array argument
7. Write a program to calculate the statistics of student marks. There are 10 student
marks stored in a floating-point array of type double: {85.0, 79.5, 81.0, 77.5,
78.0, 70.0, 72.5, 85.5, 90.5, 73.0}. Use a function to calculate the mean of the
student marks based on the equation as follows:
Use another function to calculate the standard deviation of the marks based on
the equation as follows:
Return the results of the calculation to the main function to be printed out.
8. Write a program that receives a string of characters from the user input. Then
pass the string to a function convertString to convert all characters to
capital letters. Return the string with all capital letters to the main function.
Next, pass the string with all capital letters to another function printString
to be printed out on screen.
4
9. The Fibonacci series 0, 1, 1, 2, 3, 5, 8, 13, 21 ... begins with the terms 0 and 1
and has the property that each succeeding sum of the two preceding terms. Write
a recursive function fibonacci(n) that calculates the nth Fibonacci number.
10. Write a recursive function power(base, exponent) that when invoked
returns the value of baseexponent. For example, power(3,4) = 3*3*3*3.
Assume that exponent is an integer greater than or equal to 1. Hint: The
recursion step would use the relationship
baseexponent = base · baseexponent – 1
and terminating condition occurs when exponent is equal to 1 because base1 =
base.