CPSC 110 Spring 2012
Quiz 3 Practice Problems
Instructions: The first three problems ask you to read a Pseudocode
function definition. Trace through each of them by hand, keeping track of the
variables, to determine the answer to the question.
- What value would functionA(5) return?
# Comment: A function definition that takes 1 parameter
Return: a + 100
Trace: functionA(5) returns 5 + 100 = 105
- What value would inchesToCm(10) return?
# Comment: Convert inches to centimeters
# There are 2.54 centimeters per inch
Return in * 2.54
Trace: inchesToCm(10) returns 2.54 * 10 = 25.4
- What value would mystery(3,2) return? What value would mystery(2,3) return?
# Comment: A mystery function with 2 arguments
To mystery(x, y):
Set M to x
If y < x then do:
Set M to y
x y M Return
3 2 3
2 2 mystery(3,2) returns 2
2 3 2 2 mystery(2,3) returns 2
This function computes the minimum of its arguments, a and b.
Instructions: The next three problems ask you to define a
function in Pseudocode. Model your answers after the Pseudocode
algorithms in the first 3 questions.
- Define a function named avg3 that will compute and return
the average of any three numbers. You function should have 3 arguments
named a, b, and c.
# A function to compute the average of 3 arguments
To avg3(a, b, c):
Return (a + b + c) / 3
- Define a function named kmToMiles that will convert kilometers to
miles. 1 kilometer is 0.62 miles. This function should have one argument
# A function to convert kilometers to miles.
Return km * 0.62
- Define a function named circumference that will compute the circumference
of a circle with radius r. The formula for computing circumference is
C = 2 × π × r. Use 3.14 as the value of π. This function
should have 1 argument named r.
# A function to calculate the circumference of a circle with radius r
Return r * 3.14 * 2