Docsity

Introduction to Computing - Homework 4 | CS 1301, Assignments of Computer Science

Toggle sidebar
bg1
CS 1301 – Spring 2007
Homework 4 – while homeworkDone==False:
Due: Wednesday, February 21 by 6 pm
Files to submit: hw4.py
Functions to include in hw4.py:
1. average()
2. fact()
3. pascal()
For Help:
Newsgroups
TA Helpdesk
Email TAs
Notes:
Think through the logic of each of the problems before you start coding anything.
Don’t forget to include the required comments and collaboration statement (as
outlined on the course syllabus).
Do not wait until the last minute to do this assignment in case you run into problems.
If you find an error in the homework assignment, let a TA know immediately.
If you file produces errors when “Run Module” is selected, you will receive half
credit for the part of the assignment causing the error(s).
Part 1 – average()
Remember how to computer an average of a set of numbers:
Average = Sum of numbers / Count of numbers
What is a sentinel value:
In computer programming, a sentinel value (also referred to as a flag value or signal value) is a
special value that is used to terminate a loop that processes structured (especially sequential)
data. The value should be selected in such a way that it will not be confused with legal data
values.
A programmer may choose any sentinel value, but here are some typical examples:
-999
(blank)
xxx
FALSE
END
Homework 4 CS 1301 – Spring 2007 Page 1/4
pf3
pf4

Partial preview of the text

Download Introduction to Computing - Homework 4 | CS 1301 and more Assignments Computer Science in PDF only on Docsity!

CS 1301 – Spring 2007

Homework 4 – while homeworkDone==False:

Due: Wednesday, February 21 by 6 pm

Files to submit: hw4.py Functions to include in hw4.py:

**1. average()

  1. fact()
  2. pascal()** For Help:  Newsgroups  TA Helpdesk  Email TAs Notes:  Think through the logic of each of the problems before you start coding anything.  Don’t forget to include the required comments and collaboration statement (as outlined on the course syllabus).  Do not wait until the last minute to do this assignment in case you run into problems.  If you find an error in the homework assignment, let a TA know immediately.  If you file produces errors when “Run Module” is selected, you will receive half credit for the part of the assignment causing the error(s).

Part 1 – average()

Remember how to computer an average of a set of numbers: Average = Sum of numbers / Count of numbers What is a sentinel value: In computer programming, a sentinel value (also referred to as a flag value or signal value) is a special value that is used to terminate a loop that processes structured (especially sequential) data. The value should be selected in such a way that it will not be confused with legal data values. A programmer may choose any sentinel value, but here are some typical examples:

(blank) xxx FALSE END

Write a function that keeps asking the user for numbers until they enter -999. Print out the final average of all of the numbers. Use floats for all numbers! You may not assume that the user will enter a valid number before entering -999. If the user enters -999 as the first input, the function should print : No average could be computed. (Or something similar) instead of trying to divide by zero. You will need to use a while loop to accomplish this. Do NOT use recursion. Here is a simple algorithm you can use:

  1. Initialize any variables you will need.
  2. Ask the user for input ( It is very important to ask the user for the first number before starting the loop)!
  3. While the input does not equal the sentinel value, keep looping a. Update any variables that need to change b. Ask for the next Number
  4. If numbers have been entered, compute the average and print it.
  5. Otherwise, print: No average could be computed. Sample output:

average() Enter the next number to average (type -999 to quit): 10. Enter the next number to average (type -999 to quit): 20. Enter the next number to average (type -999 to quit): 30. Enter the next number to average (type -999 to quit): - Average: 20. average() Enter the next number to average (type -999 to quit): 0 Enter the next number to average (type -999 to quit): - Average: 0. average() Enter the next number to average (type -999 to quit): 53. Enter the next number to average (type -999 to quit): 474. Enter the next number to average (type -999 to quit): - Enter the next number to average (type -999 to quit): 747 Enter the next number to average (type -999 to quit): 983. Enter the next number to average (type -999 to quit): 100. Enter the next number to average (type -999 to quit): - Average: 376.

pascal(7) 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1 For Extra Credit: Make your pascal() function print out the triangle in the more traditional, symmetric form. For example: pascal(3) 1 1 1 1 2 1 1 3 3 1 pascal(6) 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1

Final thoughts

Be sure to name your file and functions exactly as indicated. READ THROUGH THE INSTRUCTIONS CAREFULLY. If you need help, be sure to come to office hours. Good Luck! Written by Aaron Hansen, Spring 2007