CPSC 115L: Introduction to Computing Fall 2010

Laboratory 6: While Loops

October 13, 14

Preliminaries

Before coming to this lab, you should have read Chapter 7 of Downey, Iteration. In the Wednesday lab we will have a short lecture about the while statement in Python and how to properly construct a while loop.

Objectives

The main objectives of this laboratory are
  1. to learn how to design, implement and use while loops in your programs.

Introduction

For this lab we will design and develop several functions involving while loops. Some of these will be redefinitions of functions who have previously written using for loops or recursion. For each function you should provide:

These may be written as comments in your python files and handed in with the function itself and its output. Here's an example for the factorial() function.

# Pre: n is a non-negative integer
# Post:  The factorial of n will be returned.
# Algorithm:  Accumulate the product of n * (n-1) * ... * 2 * 1

def factorial(n):
   ''' Computes n! for integer n >= 0 '''
   product = 1
   i = 1                      # Initialize the loop variable
   while i <= n:              # Test the loop entry condition
      product = product * i
      i = i + 1               # Update the loop variable 
   return product

Note that in this example, the while statement is part of a larger while control structure that consists of three parts: an initialization statement, a loop entry condition, and an update statement. The initialization here occurs before the while statement. The update statement occurs within the body of the loop and it makes progress toward the loop entry condition becomeing false. All properly constructed while loops must include these three elements.

1. Functions

1.1. Write an iterative function named sum() that uses a while loop to compute the sum of 1..n. This function should have a single integer parameter, n > 0.

1.2. Write an iterative function named sum_range() that uses a while loop to compute the sum of m..n for positive integers, 0 ≥ m > n. This function should have two integer parameter.

1.3. Write an iterative function named power(x, n) that uses a while loop to compute xn for n ≥ 0. This function should have two parameters.

1.4. Write an iterative function named exam_average() that uses a while loop to compute the average of grades entered by the user from the keyboard. This function should have no parameters. The function should repeatedly prompt the user to input an exam grade. The user should be prompted to input -1 to stop the loop.

1.5. Write an iterative function named gcd(x, y) that uses a while loop to compute the greatest common divisor of x and y for x > 0, y > 0. This function should use Euclid's algorithm.

Here is a simple explanation of Euclid's GCD algorithm. The algorithm can be desribed as follows:

To compute the gcd of x and y

let rmdr be the remainder of dividing x by y
repeat until the rmdr equals 0
   let x be the previous value of y
   let y be the previous rmdr
   let rmd be the remainder of dividing x by y
return y   # Return the last non-zero remainder

In reading this algorithm, identlfy the initialization step, the entry condition, and the update step.

Documentation

Each Python script should have a comment block at the beginning with your name, date, file name, and a brief description of the program, including the problem statement. Each function should be preceded by a comment block containing its pre- and post-conditions and an outline of its algorithm and each function should include a very brief docstring that describes the purpose of the function and its parameters.

What to hand in

For each problem hand in a 1-page print out containing the definition of your function, its documentation (preconditions and algorithms), and the output you receive from running 5 tests of your function.

* CPSC 115L home page
Valid HTML 4.01!