CPSC 110 Fall 2011 Quiz 3 Practice Questions

    Instructions: The first three problems ask you to read a Pseudocode algorithm. 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 * a - a
    
    Trace: functionA(5) returns 5 * 5 - 5 =  20
    
  2. What value would mystery(3,3) return? What value would mystery(2,5) return?
    ProblemSolution
    # Comment: A mystery function
    
    To mystery(X, Y):
       If X = Y then do:
          Return X * Y
       If X > Y then do:
          Return X - Y
       else do:
          Return Y - X
    
    X        Y        Return
    ------------------------
    3        3        9       mystery(3,3) returns 9
    
    2        5        3       mystery(2,5) returns 3
    




    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.

  3. Define a function named areaOfCircle that computes the area of a circle with radius, r. Recall that A = π r2. You can use 3.14 as the value of π.

    # A function that computes the area of a circle with radius R
    
    To areaOfCircle(R):
       Return 3.14 * R * R
    
  4. Define a function named mpg that calculates miles per gallon. This function should take two arguments, mi for miles traveled, and g, for gallons of gas used. It should the average number of miles per gallon used by the car.

    # A function to compute mile per gallon, given mi for miles and g for gallons
    
    To mpg( mi, g): 
       Return mi / g