A202 / I211 Assignment 1

Chaos and Future Value

Due Thursday, September 9th, 3:30 PM

In lab

Login and double click on the CFS desktop icon, patiently wait for the server to get around to you, and then login to it also. This opens an explorer window for m:\, your CFS virtual disk drive. If neither you do not have a CFS account that works, use the directory c:\TEMP instead of m:\, and be very sure to copy your work to a floppy before you leave. It's a good idea to keep a copy of your work on a floppy in any case.

You might like to create a shortcut to the A202/I211 web and save it in your CFS as follows. Right click the mouse in your CFS explorer window (with m:\ in the title bar) and select New > Shortcut in the popup menu. Enter http://www.cs.indiana.edu/classes/a202-hayn (or cut and paste it from the IE location text box) into the text window of the shortcut dialog, click next, enter A202 (or I211) in the name box, and click finish. Now double-click your new link to view the course web.

Create the directory m:\lab1 for this lab. (Right click in the explorer window and select New > Folder. Then right-click the new folder and select Rename to change the name to lab1.) It is highly advisable to create a directory like this for each lab and assignment. Using characters other than letters, numbers, and spaces, in directory (folder) names can cause problems for java (and many other kinds of systems).

Run the command Start > Programs > Development Tools > Python 2.3.4 > IDLE (Python GUI) to start the Python IDLE (development environment shell.) OR if UITS hasn't got it together yet to install this as requested, use the old Python IDE as follows: Start > Programs > Development Tools > ActivePython 2.2 > PythonWin IDE. This works approximately like the IDLE demonstrated in class with the differences highlighted below.

After the >>> prompt, enter the print statements in programming exercise 1 on pages 22 and 23 of your text. Think before each one what you expect the output to be. (You will not be doing most of the text's programming exercises in lab in the future, but it is a good idea to try them on your own, and thing about them first.)

Position the cursor somewhere above the last line of the shell window and try typing. Nothing happens! Press return and notice that the line the cursor was on is copied to the last line. The cursor must be on the last line to enter text in the Python shell window. This and other strange or troublesome features are noted in the System Notes page of the course web, linked from the contents panel on the left. Be sure to read the first note on that page before trying to print anything.

Now do programming exercise 2, with a bit extra at the end. Here is what to do, step-by-step:

  1. In the Python Shell window type ctrl-N (the File > New command) to open an IDLE editing window for the chaos program. With ActdiveState Python, select Python Script in the dialog that pops up.

  2. You do not have to type the chaos program, or any program in your text. Switch to your web browser, viewing the class web, and click on the Text code link in the contents panel on the left. Then click chapter1 and chaos.py to view the program.

  3. Type ctrl-A ctrl-C to execute the Edit > Select All and Edit > Copy commands.

  4. Switch back to the IDLE editor window and type ctrl-V to paste the program into the editor, and then ctrl-S to save the program to a file named chaos.py in your m:\lab1 directory. Since the file has a .py suffix, the editor now knows it is a Python source file, and it colorizes it so it is easier to see the syntactic structure of the program (keywords, comments, and literals are in distinct colors that stand out, while variable and operator names are black).

  5. Press F5 to execute the Run > Run Module command. Or with the ActiveState Python IDE, click the button with the running-person icon.(A Python source file is a module.) The Python Shell window pops up, is restarted, and executes the chaos program, waiting after the prompt to enter a number. So enter a number as requested and press <enter>. (Since ou always have to press <enter> after entering anything in the Python shell, that won't be mentioned again.)

  6. To try another number you could go back to the editor window and press F5 again, but there is a better way: just type main() after the shell's >>> prompt. Try it, and enter a different number. This works because when you run a module with F5 it is as if the file's contents had been typed into the shell, so the definition of the main function is in the shell's environment.

  7. Suppose we would like to try the chaos program with a lot of numbers. We can automate that even further. Go to the editor window, position the cursor at the end of the last line of the main function (the end of the second print statement), and press <enter>. Notice that the cursor is now indented at the level of the last print statement, which is inside the for loop. This is not what we want, so press <back space>. The cursor should now be indented at the level of the beginning of the for statement.

  8. Now type main() and press F5. After saving the file, you're back in the shell. Every time you enter a number, it prints some chaos numbers and asks you to enter another number. Try it a few times. This is great for testing the program, but you're stuck in an infinite sequence of recursive calls to the main function.

  9. To get out of this infinite regression, type ctrl-C. (This usually works, but sometimes you have to use the Shell > Restart Shell command to kill a wedged program.)

  10. Go back to the editor, place the cursor in the whitespace before the main function call you just added and press <tab>. The function call is now indented the same as the last print statement, so it is now inside the body of the for loop. Run it again (F5) to see what this does. Enter a few numbers. Why is it behaving this way? Use ctrl-C again to terminate the program.

  11. Go back to the editor and remove the line with the new call to main. Then delete the colon at the end of the for line and press <enter>. Notice that the cursor is indented at the same level as the line above, indicating that whatever is entered at that point is not part of the for loop body, as it should be. When the indentation is wrong, it is an immediate clue that something is wrong. In many cases because you forgot a colon at the end of the preceding line. Press F5, and notice what a syntax error warning looks like. Now press ctrl-Z a couple of times invoking the Edit > Undo command to restore the colon you deleted to run this experiment.

  12. Read and comply with points one and two of the course style rules and guidelines. (Any module that ends with main() is an application.)

Next do the following programming exercises 3, 4, and 5. (You don't have to write the paragraph asked for at the end of exercise 3, but it is a good thing to think about.) Unless you are told otherwise, whenever you are asked to do several exercises that involve the same program initial program, do them cumulatively. That is, start each exercise with the program you wrote for the previous one. Be sure to test each exercise as soon as you are done with it. It is always best to test program changes as soon as it is practical to do so. That way you know what part of the program is most suspect if something is wrong.

When you have completed exercise 5, or 15 minutes before the lab ends, whichever comes first, submit your chaos.py file as lab 1 in Vincent, by carefully following the instructions on the course web's Vincent page, linked in the contents panel on the left. If you have added the course late or otherwise have confirmed with your lab instructor that your are not in the Vincent system, this one time only you may email your file to your lab instructor.

Even if you have not completed the lab portion of an assignment, always submit whatever you have done before the lab ends if the lab portion of the current assignment asks for a submission. Otherwise, you may not get credit for attending the lab, which may count as a hand-in. If you have made a good effort, you will get credit even if you have not finished the lab.

If you have finished the in-lab portion of an assignment before the end of a lab session you may leave, but you are strongly encouraged to stay and start work on the main portion of the assignment. That way you can get help right away if you need it.

The CFS server is occasionally unusable, so you are strongly advised to carry a floppy disk to every lab. Before you leave each programming session using CFS, if not more often, copy any assignment work to a floppy disk. It only takes a few seconds, and that way you can continue work later even if CFS is not cooperating at the time.

Assignment

Create a CFS directory named a1 for this assignment. (This won't be specified in the future, but you should similarly create a new directory named for each assignment you start work on.)

Read the first seven items in the course style rules and guidelines. Pay special attention to the rules, especially the first rule. You aren't expected to follow all the guidelines perfectly in the first assignment, but start getting used to them. They will be increasingly important in future assignments for both making your programs easier to understand (and hence get working) and for getting a good grade.

Do programming exercises 5, 6, and 7 of chapter 2. (As noted above, these are to be done cumulatively, and you can start by grabbing the text's version of futval.py from the course web.) Remove the first print statement, which is not accurate after the program is modified.Your program should prompt for:

  1. the amount to invest each year,
  2. the annual interest rate,
  3. the number of compounding periods per year, and
  4. the number of years,

in that order. Assignments that take keyboard input will be tested automatically, and if they do not accept the right number of inputs in the right order the tests will all fail and you will loose substantial credit.

Assume the annual investment is made in one chunk at the beginning of each year. Hint: use another for loop nested in the body of the existing one to do the compounding of interest in each period.

Incorporate the values that are input in your programs output. For example:

Enter the amount invested each year: 100
Enter the annual interest rate: .05
Enter the number of compounding periods per year: 4
Enter the number of years: 20

The value in 20 years,
compounded 4 times a year,
with an annual interest rate of 0.05
is 3509.97318426

The final value should be the only number on the last line. Do not be concerned now that the answer prints with a degree of precision that is not appropriate for this kind of calculation. You will learn soon how to fix that sort of thing.

Unless an assignments says you should check for bad input, you may assume input values are sensible. In this case, that includes assuming the number of periods and the number of years are integers that are 1 or greater. Assignments will often include one or more examples such as the one above, but just because your program gives the right answer in the given example(s) does not necessarily mean it is correct. Think about your programs carefully and test them thoroughly.

When you are done, submit your final futval.py file as a1 using Vincent. If you cannot finish all steps of the assignment, be sure to submit, before the due time, a version of your file that does as much as you could get working. You may include as comments, for some partial credit, any code you wrote that does not work. Be sure that whatever you submit loads without error messages, or you will get very little credit.