CPSC 110 Spring 2012 Quiz 4 (Redo)
Answer Key

    Instructions: The first problem asks you to read a function and determine what it does. The second problem asks you to write a function given a description.

  1. What value would function(4, 7) return? Show your work.
    # Comment: A function with 2 parameters
    
    To function(M, N):
      Set global Var to 0
      While M <= N do:
         Set Var to Var + M
         Set M to M + 1
      Return Var
    















  2. Define a function named sumOdds(aList) that will compute the sum of the odd numbers in aList. For example, if we call the function with the list [1,2,3,4,5] as in
        sumOdds( [1,2,3,4,5] )
    
    it will return 9 because 1 + 3 + 5 = 9.



     

    Instructions: The next two problems ask you to read and write loop algorithms.

  3. Suppose you have a variable named list that contains the following values: [10,20,3,4,5,20,7,8,9]. What would be the final value of Var as a result of the following algorithm?
    # Comment: What is the final value of Var
    
       Set global Var to 0
       For each item in list do:
          If item > length(list) then do:
             Set Var to Var + item
    
    















  4. Convert the loop algorithm in problem #3 (above) to an equivalent while loop. Be careful about indentation.