Lab Exercise 9

A Brief Exploration of Functions


CS 1510
Introduction to Computing


Introduction

Create a directory on your USB device for this lab, say, lab08, and launch IDLE.

At the end of the lab, you will submit a responses.txt file that shows your answers to questions and the output of your programs in action. Download this file and use it to record answers and outputs for each task.



Task 1: Predicting Function Results

For each of the following programs, predict the output that is displayed to the screen when each is run.

Do not run the program. Instead, trace it by hand.

If a line of code will cause an error, predict the specific error, then assume that the line has been commented out before moving on to the next line.

Record your predictions in your responses file.



Task 2: Observing Function Results

For each of the following programs, execute the output that is displayed to the screen when each is run.

If a line of code will cause an error, record the specific error, then comment the line out and re-run the program.

Record your results in your responses file.

Then, explain any cases where the actual output differed from the output you predicted. That is, what is true about how functions work that you didn't understand (or remember) before seeing the program run? Two common issues are how the program passes parameters to the function and how the function returns its value to the program.

Record your explanations in your responses file.



Task 3: Computing Means

When most people think of the "average" of two numbers, they think of the arithmetic mean:

              x + y
    average = -----
                2

However, there are other kinds of "average". For example, there are the geometric mean:

    average = square root of (x * y)

and the harmonic mean:

       1                             1     1
    ------- = the arithmetic mean of - and -
    average                          x     y

Download this partially-completed program and write the bodies of the three functions that compute these means.

Write the functions one at a time, uncommenting the line that calls the function as you complete it.

Next, run your completed program with these inputs:

Record your results in your responses file.



Task 4: Hashing a String

In many applications, we need a way to convert an arbitrary string into an integer. Such applications range from creating ways to look up data quickly to determining whether someone has entered a valid password.

One simple way to "hash" a string is to add up the ASCII values of its characters, and then find the remainder from dividing the sum by an integer. For a variety of reasons, this integer is often a prime number.

For example, suppose that we chose 19 as our divisor. This algorithm would compute a value of 17 for kurt and a value of 12 for vonnegut.

Download this partially-completed program and write the function hash() that completes the program.

Hint: recall that you can use the ord() to find the ASCII value of a character.

Next, run your completed program with these inputs:

Record your results in your responses file.



Finishing Up

Make sure that your program files are complete and saved. Save your responses.txt file.

Submit your files for grading on the electronic submission system, at lab09 -- A Brief Exploration of Functions.

As always, make sure you see the verification screen that says The files listed above were uploaded.

If you need any help, let me know.



Eugene Wallingford ..... wallingf@cs.uni.edu ..... October 22, 2014