Difference between revisions of "CSC111 Homework 2 2014"
(→Problem #1) |
|||
Line 2: | Line 2: | ||
---- | ---- | ||
<bluebox> | <bluebox> | ||
− | You must work in Pair-Programming mode on this homework. This homework is due on Thursday evening, 2/13/14 at midnight. | + | You must work in Pair-Programming mode on this homework. This homework is due on Thursday evening, 2/13/14 at midnight. Each problem is worth 1/3 of the total grade. |
</bluebox> | </bluebox> | ||
Revision as of 11:37, 6 February 2014
--D. Thiebaut (talk) 19:41, 4 February 2014 (EST)
You must work in Pair-Programming mode on this homework. This homework is due on Thursday evening, 2/13/14 at midnight. Each problem is worth 1/3 of the total grade.
Problem #1
- First watch this video with some comments I had after Wednesday's Lab 2. This will help you better focus on the homework.
- Call your program hw2a.py
- Using the same approach as you took in Lab 2, write a Python program that asks the user for an amount of money (dollars) that can include cents, and outputs a breakdown of this amount into bills and coins.
- This time, however, you must use the string-formatting output provided by the % operator, as introduced in Section 2.5.3 of the textbook. You cannot use the concatenation method we used to print your output. You must use the %-operator.
Here is an example of the output your program you emulate. Note the the line between the bills and the coins.
Please enter the amount of money you want to withdraw: 200.56 +------------------------------------+ | Total amount withdrawn: $ 200.56 | | | | 10 $20-bill(s) | | 0 $10-bill(s) | | 0 $5-bill(s) | | 0 $1-bill(s) | |------------------- | | 3 quarter(s) | | 0 dime(s) | | 0 nickel(s) | | 1 cent(s) | +------------------------------------+
It is totally acceptable to assume that the amount of money will never be more than $9,999.99, and therefore you can assume that the width of the box is constant and will always be large enough to accommodate an amount as large as $9999.99.
- Make sure your program contains a header. The header is at the very top of the program, and is made of several comment lines.
In it you want to find the following information:
- the name of the program
- the name of the programmer(s), along with the 2-letter Id we'll use for this class.
- the date the program was written
- a description of what the program computes. What inputs it expects. Whether it will run for all possible inputs or if there are known cases for which it might not run.
Submission
- The submission link is cs.smith.edu/~thiebaut/111b/submit2.php
Problem #2
Write a program call hw2b.py that will compute the total balance of an investment growing at a fixed yearly interest rate.
The program should ask the user for two numbers:
- A initial investment, entered as a real number ( 100, 2500.50, for example)
- An interest rate, expressed as a real number less than 1. For example a 5% rate would be entered by the user as 0.05.
Your program will then output 10 lines representing the total balance of the investment after 1, 2, 3, ... 10 years.
We will forget that we have seen loops (the first week was just a quick exploration of Python and logic), so you will need 10 separate print statements to output the 10 balances.
Below is an example of how your program should operate. The user enters 1000 and 0.045 as the two inputs. The program outputs the rest.
Please enter the initial balance : 1000
Please enter the interest rate (enter 0.03 for 3%): 0.045
Table for an investment of $1000.00 at a rate of 4.5%
Balance after 1 year: $1045.00
Balance after 2 year: $1092.02
Balance after 3 year: $1141.17
Balance after 4 year: $1192.52
Balance after 5 year: $1246.18
Balance after 6 year: $1302.26
Balance after 7 year: $1360.86
Balance after 8 year: $1422.10
Balance after 9 year: $1486.10
Balance after 10 years: $1552.97
Submit your program using the same link you used for Program 1.
Problem #3
Create a new program called hw2c.py. This program will contain 4 different code sections that will output the solutions for the first four problems of Homework 1. However, this time you will be using the %-operator for formatting output strings.
Note: the %-operator for formatting outputs is covered in Section 2.5.3 of the textbook.
Submit your program using the same link as for Problem 1 above.
Additional Important Information
Make sure that you include a header in your program. A header should contain
- the name of the program,
- the name of the programmer with the 2-letter Id (When you work in pair, include both names, one above the other, each followed by the 2-letter Id).
- the current date,
- a description of what the program does. Right now the programs are simple enough that a line or two is sufficient.
Also, add comments in your program to indicate the logical sections.
Below is an example of a program you should use as an example:
# ExampleProg.py
# Mickey Mouse (zz)
#
# This program outputs a list of name with boxes around them.
# -----------------------------------------------------------
# Setup the list of names
list = [ "Minnie", "Donald", "Mickey", "Pluto", "Duffy" ]
BOXLEN = 20 # the size of the boxes
# display each name found in the list in its own box
bar = "+" + "-"*BOXLEN + "+"
for name in list:
# print top bar
print( bar )
# compute spaces at end of name
spaces = " " * ( BOXLEN-1 - len(name) )
# print name
print( "| " + name + spaces + "|" )
# print bottom bar plus blank line
print( bar )
print()