Homework #7

Due: March 19, 2012Points: 100

As for all earlier assignments, please put refinements for all your programs into one file called “refinements.txt” or “refinements.pdf”, and error journal entries for all your programs into one file called “errors.txt” or “errors.pdf”.

In the example outputs, what your program outputs is in typewriter font, what the user types is in italicized typewriter font, and the user typing “enter” or “return” is represented by the character “↵”. You don’t need to change the font, use italics, or show the “↵” in your output.

In all programs, simply exit if the user types the end of file character (control-D on most systems).

  1. (30 points) Write a function to determine whether a year, given as the argument, is a leap year. A year is a leap year if it is divisible by 4, unless it is divisible by 100 and not 400. So 2000 was a leap year, but 2100 and 2200 will not be. Then write a program that asks the user to enter a year and uses the function you wrote to determine whether the year is a leap year. The program then prints the result and exits.

    For example:

    Please enter a year: 2012
    2012 is a leap year.
    and
    Please enter a year: 2013
    2013 is not a leap year.

    Submit: Please turn in your program in the file “leap.py”.

  2. (30 points) The body mass index (BMI) is calculated as a person’s weight (in pounds) times 720, divided by the square of the person’s height (in inches). A BMI in the range 19–25, inclusive, is generally considered healthy for most people (as always, consult your doctor to determine if your BMI is considered healthy for you). Write a program that calculates a person’s BMI and prints a message telling them whether they are above, within, or below the healthy range given above. Your program is to prompt for the person’s weight and height, as below.

    For example:

    Enter weight (lb): 199
    Enter height (in): 67
    Your bmi of 31.92 is above the healthy range of 19-25
    and
    Enter weight (lb): 160
    Enter height (in): 68
    Your bmi of 24.91 is within the healthy range of 19-25
    and
    Enter weight (lb): 120
    Enter height (in): 72
    Your bmi of 16.67 is below the healthy range of 19-25

    Submit: Please turn in your program in the file “bmi.py”.

  3. (40 points) The greatest common divisor (GCD) of two integers m and n is the largest number that divides evenly into both of them. Write a program that asks the user to enter two integers, then calculates and displays their GCD. Your program may not be recursive; it must use iteration. (See the hint below.)

    For example:

    Please enter the first number: 12
    Please enter the second number: 15
    The greatest common divisor of 12 and 15 is 3
    and
    Please enter the first number: 16
    Please enter the second number: 40
    The greatest common divisor of 16 and 40 is 8

    Submit: Please turn in your program in the file “igcd.py”.

    Hint: The classic algorithm for computing the GCD, known as Euclid’s Algorithm, goes as follows. Let m and n be variables containing the two numbers. Divide m by n. Save the divisor (that is, n) in m, and save the remainder in n. If n is 0, stop; m contains the GCD. Otherwise, repeat the process, starting with the division of m by n.


A PDF version is available here.
ECS 10, Basic Concepts of Computing
Winter Quarter 2012