CSC 111: Introduction to Computer Science
HW 3: Data Types, Math, Date and Random Libraries
Due Wednesday Feb 16 Midnight, via Moodle
SUBMIT as HW3_Fib.py and HW3_Snow.py
Office Hours Monday 2-3; Tuesday 1:30-2:30
Feedback on homework programs and samples
Part 1: Fibonacci
Write a program to find and print out the Fibonacci sequence of n numbers. See problem 16 on page 74 of the text,
or simply Google Fibonacci. Also graph the results in a manner you find interesting and visually clear and pleasing.
Presenting information in a manner that communicates your message effectively to others is a very high level skill. You only
need to use the elements of the graphics library that we have already seen, but do work to make your graph as clear and complete
as possible, given what you have learned so far. Next week in class we will see more operations
that will let you label things with text.
As with part of HW 1 from the beginning of the semester, for this part of HW 3, completing the task above,
using the same coordinates for the
graph, regardless of what value of n is input, will earn a score of B+/A-. To receive a score of 'A,' you
will also need to dynamically set the graph coordinates so that they are scaled differently depending upon what
value of n is input. For example, if the maximum value in the Fibonacci sequence is 8, then the y-axis could be
scaled to 8, or perhaps 10. If the maximum Fibonacci number is 55, then the y-axis should be scaled to perhaps 60.
The x-axis will also need to be scaled appropriately. We do not have the tools, yet, to do this part of the HW
elegantly. You only need to show some thought for this, that will work well for the first part of the Fibonacci
sequence, and will still work ok as the sequence grows. You will also need to use the "trial and error" method discussed
in the text, and this part of the HW should help you become more comforable with trial and error in programming.
The objective of this exercise is for you to
- Get some practice using the math library and/or built in math operators.
- Write a program that uses the accumulator for loop
- Gain more experience with the graphics library
- Gain some comfort with trial and error in programming
Note that there is a difference between (i) using the web as a resource to help think through algorithms, and (ii)
plagiarism. You can plagiarize computer code just as you can plagiarize prose. Do feel free to use the web
as a resource, but just as with the written word, you need to present your own work, and if you do use an
outside source, cite that source by including the URL in your comments. Finally, you can complete this assignment
using only what we have learned in class - do not use constructs that you find on the web, and do not fully
understand.
SUBMIT as HW3_Fib.py
Part 2: Random Snow Fall and Punxsutawney Phil
Investigate the 'random' library. Using this library, generate numbers that you will use as a prediction of the
number of inches of snow that will melt each day over the next four weeks. Alse generate random numbers for the number of
inches of snow that will fall and accumulate each day over the next four weeks. Will your program show us that spring will
be coming early, or are we going to get buried under never-ending mountains of snow?
Graph the results in a way that
communicates three values - the snow melt, the snow fall and the net amount of remaining snow, each day.
You need to determine:
- How to graph the results in a way that will make sense to anyone looking at your graph. One issue we cannot deal with
well, yet, is what to do with your data when the net amount of snow melting is greater than the net amount of snow falling.
The most natural way to address this issue is to use an if-else program flow construct, which we have not yet
introduced. Those of you who have more programming experience are free to use the if-else, but for this assignment
you are free to graph negative net snow, and we can interpret that as the ground thawing out and bulbs getting
ready to bloom.
There are two main functions available to generate random numbers. One generates floating point numbers, and the other
generates integers.
- Use random(), which returns a float, for snow melting. Random() will return a number between 0.0 to 1.0.
This is a very typical way for computer languages to generate random numbers. It is standard
practice to have a function that provides a random number between 0.0 and 1.0, and then the programmer
must manipulate the random numbers generated into the type (perhaps an integer) and range (perhaps an
integer between 1 and 12) desired.
- Use randrange(), which returns an int, for snow fall.
- For full 'A' credit in this part of the homework, use the time.sleep() method from the lab as your
data is plotted, so the plotting of the snow fall and snowmelt occurs with a short (perhaps 0.5 or 1 second)
delay between each line or point being plotted. This is a very simple type of animation that will allow
the user to watch the accumulation and/or melting of snow gradually, as your program executes.
SUBMIT as HW3_Snow.py