CPSC 110 Spring 2012 Quiz 3 Practice Problems Solutions

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.

1. What value would functionA(5) return?
ProblemSolution
```# Comment: A function definition that takes 1 parameter

To functionA(a):
Return: a + 100
```
```Trace: functionA(5) returns 5 + 100  = 105
```
2. What value would inchesToCm(10) return?
ProblemSolution
```# Comment: Convert inches to centimeters
# There are 2.54 centimeters per inch

To inchesToCm(in):
Return in * 2.54
```
```Trace: inchesToCm(10) returns 2.54 * 10  = 25.4
```
3. What value would mystery(3,2) return? What value would mystery(2,3) return?
ProblemSolution
```# Comment: A mystery function with 2 arguments

To mystery(x, y):
Set M to x
If y < x then do:
Set M to y
Return M
```
```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.

4. 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
```

5. Define a function named kmToMiles that will convert kilometers to miles. 1 kilometer is 0.62 miles. This function should have one argument named km.
```# A function to convert kilometers to miles.

To kmToMiles(km):
Return km * 0.62
```

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

To circumference(r)
Return r * 3.14 * 2
```