CPSC 110 Fall 2011 Quiz 2 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. Suppose you have a global variables named Var and N. What value would Var have after the following Pseudocode segment?
    ProblemSolution
    Set Var to 100
    Set N to 3
    Set Var to Var - N
    If Var < 97 then do:
       Set Var to 0
    else-do:
       Set Var to 1
    
    Var   N   Var < 97
    ------------------
    100   3     
    97    3     false
    1                    So the answer is Var = 1 after the algorithm runs
    
  2. What values would Var and N have after the following Pseudocode segment?
    ProblemSolution
    Set Var to 1
    Set N to 0
    While Var < 16 do:
       Set Var to Var * 2
       Set N to N + 1
    
    Var   N   Var < 16
    ------------------
    1     0     true
    2     1     true
    4     2     true
    8     3     true
    16    4     false  So the answer is Var = 16 when the loop finishes
    
  3. Consider the following procedure. What value would Var have after the DoSomethingInteresting procedure is called?
    ProblemSolution
    # Comment: A mystery procedure in Pseudocode
    
    To DoSomethingInteresting
      Set Var to 0
      Set N to 1
      While N < 10 do:
          Set Var to Var + N
          Set N to N + 2
    
    Var   N   N < 10
    ------------------
    0     1     true
    1     3     true
    4     5     true
    9     7     true
    16    9     true
    25    11    false   So Var = 25 after this procedure is called.
                        The procedure computes the sum of the odd numbers between 1 and 10
    




    Instructions: The next three problems ask you to write a Pseudocode algorithm. Model your answers after the Pseudocode algorithms in the first there questions.

  4. Suppose you have the global variables A, B, and C. Write a Pseudocode algorithm that will add them together and store the sum in the variable Var

    # Comment: An algorithm that adds A, B, and C giving Var
    
      Set Var to 0                            
      Set Var to Var + A       or  Set Var to A + B + C
      Set Var to Var + B
      Set Var to Var + C
    
  5. Suppose you have a variable N and a variable M. Write an if/else algorithm that tests whether N and M are equal. If they are, set the variable Var to 1. Otherwise set it to 0.

    # Comment: Sets Var to 1 if M=N or to 0 of M <> N
    
      If M = N then do:
         Var = 1
      else=do:
         Var = 0
    

  6. Suppose you have variables named Var and N. Write a pseudocode loop algorithm that will use these two variables to multiply Var by itself 10 times. For example, if Var is 2, then your algorithm will multiply 2 * 2 * 2 ... * 2, 10 times, giving 1024.

    AlgorithmTrace
    # Comment: Multiplies Var by itself N times. 
    # It is not necessary to know the value of Var, but here we let Var be 2.
    # And here we need a third variable to store the result. Call it Product.
    
       Set Var to 2
       Set Product to 1
       Set N to 1
       While N <= 10 do:
          Set Product  = Product * Var
          Set N to N + 1
    
    Var      Product     N    N <= 10
    --------------------------------
    2        1           1     true
    2        2           2     true
    2        4           3     true
    2        8           4     true
    2        16          5     true
    ...      ...         ...   ...
    2        512         10    true
    2        1024        11    false