CSE 131 Module 3: Iteration

Given Quiz

Name (print) ___________________________________   Lab Section you attend______________

CSE131 Quiz 3
19 Sept 2008

  1. One test of the fairness of a random-number generator is the average of a sequence of random numbers. Recall that Math.random() returns a random double r such that 0 ≤ r < 1. Fill in as much detail as you can in the method below, using iteration to obtain the average of a sequence of num random values from Math.random(). Declare other variables you need within the method, and make sure the average ends up in the avg variable.
    public static void avgRandom(int num) {
       System.out.println("Average is: " + avg);
  2. Consider the loop below, intended to compute ans as the product of a and b:
    // return the product of a and b, which must be supplied as positive integers.
    public static int mpy(int a, int b) {
       int ans = 0,
             i = b;
       while (i != 0) {
           i = i - 1;
           ans = ans + ____________
       return ans;
    1. What is the loop's termination condition?
    2. Fill in the blank in the above code so that the loop correctly computes the desired product.
    3. In the method above, place a large dot (·) at each point where a loop invariant should be true.
An optional extension is available for you to pick up as you hand in this quiz. You can take the extension home, work on it (by yourself only please) and "demo" it to the professor in Studio and Lab next week.

Solution not yet available.