Introduction to Computer Science: The Way of the Program — Homework 1

Due by 11:59pm Tuesday, September 12

Reading


Programming Exercises

Do the following exercises in the order shown. Using IDLE, create a new blank file named assign1.py and put all of your program definitions for this assignment in the file, including the definitions from lab specified below. You are free to use any of the code that we discussed in class as a basis or guide for writing your own programs.

  1. Finish the following programs from Lab 1. You may need to copy-and-paste the definitions you wrote during lab into your new assign1.py file.

  2. The Konditorei coffee shop sells coffee at $10.50 a pound plus the cost of shipping. Each order ships for $0.86 per pound + $1.50 fixed cost for overhead. Write a program called coffee() that asks the user for an amount of coffee (in pounds) and prints out the cost of the order, rounded to 2 decimal places. Examples:

    >>> coffee()
    How many pounds of coffee would you like? 7
    The total cost of your order is $ 81.02
    
    >>> coffee()
    How many pounds of coffee would you like? 0.6
    The total cost of your order is $ 8.32
    
  3. Write a program called flash() that determines the distance to a lightning strike in feet, based on the time elapsed between the flash and the sound of thunder. Assume that the speed of sound is 1100 feet/second and 1 mile is 5280 feet. The program should also report the distance in miles. The output should look like this:

    >>> flash()
    Elapsed time in seconds between flash and thunder: 3
    You missed getting zapped by about 3300 feet
    which is 0.625 miles
    
  4. The average adult heart beats about 60 to 70 times a minute at rest. Write a program called heart() that asks the user how old they are, and computes the approximate number of heartbeats they have had in their lifetime so far. Your program should also ask for the value to use for the average heart rate, in beats per minute. (For simplicity, we won't count the heartbeats that have occurred since the user's last birthday.) For example:

    >>> heart()
    What is your age? 30
    Average heartbeats per minute? 65
    That's about 1024920000 heartbeats so far.
    Keep up the good work!
    
  5. Using your addSequence() program from lab as a guide, write a program called addcubes() that asks the user for a starting number and an ending number, and adds up the cubes of all of the numbers in the specified range (inclusive). Your program should report the starting and ending numbers along with the final sum, as shown below. For example, 23 + 33 + 43 is 99, and 53 + 63 + 73 + 83 + 93 + 103 is 2925.

    >>> addcubes()
    Enter start value: 2
    Enter end value: 4
    The sum of the cubes from 2 to 4 is 99
    >>> addcubes()
    Enter start value: 5
    Enter end value: 10
    The sum of the cubes from 5 to 10 is 2925
    
  6. Write a program called addinputs() that adds up a series of numbers entered by the user. The program should first ask the user how many numbers to enter, then it should prompt the user for each number, one by one, and finally report the total sum. An example of how the program should work is shown below. Hint: the structure of this program will be very similar to addcubes(), except that you will need to use an eval/input statement inside the loop to ask the user for the next number to add.

    >>> addinputs()
    How many numbers to enter? 5
    What is the next number? 10
    What is the next number? 6
    What is the next number? 12
    What is the next number? -9
    What is the next number? 17
    The sum of those numbers is 36
    
  7. Write a program called average() that computes the average of a series of numbers entered by the user. The program should first ask the user how many numbers to enter. It should then repeatedly prompt the user to enter the next number, keeping track of the running total. Finally, after the appropriate number of values have been entered, the program should calculate and report their average. Hint: the structure of this program will be very similar to addinputs(). For example:

    >>> average()
    How many numbers to enter? 4
    What is the next number? 8
    What is the next number? 12
    What is the next number? 7
    What is the next number? 15
    The average of those numbers is 10.5
    
  8. The program flipnums() prints out a sequence of numbers with alternating signs, starting from some positive value, as shown below:

    >>> flipnums()
    Enter a positive starting value: 11
    How many numbers to print? 5
    11
    -12
    13
    -14
    15
    

    To write this program, fill in the blanks in the code template below, which is based on the outline of a general for-loop discussed in class. The looping process uses two variables, number and sign. The variable number should keep track of the current positive value (11, 12, 13, etc.), while sign should keep track of the current sign, represented as 1 or -1. Except for filling in the five blanks, you are not allowed to modify or add any code or variables to this program, or to change its structure. Hint: this program is very similar to the flipsign program we wrote in class.

    def flipnums():
        start = eval(input("Enter a positive starting value: "))
        howMany = eval(input("How many numbers to print? "))
    
        # set up variables for the first loop cycle
        number = _____________________________________
        sign = ____________________________________
    
        for c in range(howMany):
    
            # do the work for this loop cycle
            _________________________________
    
            # get variables ready for the next loop cycle                                                   
            number = _______________________________________
            sign = ______________________________________
    
  9. An interesting way of computing the square of any positive number n without using multiplication is to just add up the first n odd numbers starting from 1. For example, 42 = 1 + 3 + 5 + 7 = 16. Using this idea, write a program called square() that asks the user for a number n and then computes n2 using a for-loop with addition only. You are not allowed to use Python's multiplication (*) or exponentiation (**) operators, or math library functions for this problem. For example:

    >>> square()
    Enter a number: 4
    4 squared is 16
    

    Hint: use a variable called total to keep track of the running total, and another variable called odd to keep track of the current odd number. Increment odd by 2 on each loop cycle, following the outline of a general for-loop:

    # set up variables for the first loop cycle
    
    for c in range(numberOfCyclesYouWant):
    
        # do the work for this loop cycle
    
        # get variables ready for the next loop cycle
    


Turning in Your Homework