Difference between revisions of "CSC111 Homework 2 2014"
(→Problem #1) |
|||
(19 intermediate revisions by the same user not shown) | |||
Line 2: | Line 2: | ||
---- | ---- | ||
<bluebox> | <bluebox> | ||
− | You must work in Pair-Programming mode on this homework. This homework is due on | + | You must work in Pair-Programming mode on this homework. This homework is due on Friday evening, 2/14/14 at midnight. Each problem is worth 1/3 of the total grade. |
</bluebox> | </bluebox> | ||
Line 8: | Line 8: | ||
<br /> | <br /> | ||
− | =Problem #1= | + | =Problem #1 = |
<br /> | <br /> | ||
* First watch this video with some comments I had after Wednesday's Lab 2. This will help you better focus on the homework. | * First watch this video with some comments I had after Wednesday's Lab 2. This will help you better focus on the homework. | ||
Line 15: | Line 15: | ||
<br /> | <br /> | ||
* Call your program '''hw2a.py''' | * Call your program '''hw2a.py''' | ||
− | * Using the same approach as took in [[CSC111 Homework 2 2014|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. | + | * Using the same approach as you took in [[CSC111 Homework 2 2014|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. | * 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. | 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. | + | Please enter the amount of money you want to withdraw: '''200.76''' |
+------------------------------------+ | +------------------------------------+ | ||
− | | Total amount withdrawn: $ 200. | + | | Total amount withdrawn: $ 200.76 | |
| | | | | | ||
| 10 $20-bill(s) | | | 10 $20-bill(s) | | ||
Line 47: | Line 47: | ||
==Submission== | ==Submission== | ||
− | * The submission link is [http://cs.smith.edu/~thiebaut/111b/submit2.php cs.smith.edu/~thiebaut/111b/submit2.php] | + | * The submission link is [http://cs.smith.edu/~thiebaut/111b/submit2.php cs.smith.edu/~thiebaut/111b/submit2.php]. Your 2-letter Id can be found [[CSC111_2-Letter_Ids_2014 | here]]. |
<br /> | <br /> | ||
=Problem #2= | =Problem #2= | ||
− | + | <br /> | |
Write a program call '''hw2b.py''' that will compute the total balance of an investment growing at a fixed yearly interest rate. | 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: | The program should ask the user for two numbers: | ||
Line 62: | Line 62: | ||
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. | 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. | ||
− | + | <br /> | |
+ | <br /> | ||
<source lang="text"> | <source lang="text"> | ||
Please enter the initial balance : 1000 | Please enter the initial balance : 1000 | ||
Line 79: | Line 80: | ||
Balance after 10 years: $1552.97 | Balance after 10 years: $1552.97 | ||
</source> | </source> | ||
− | + | <br /> | |
+ | <br /> | ||
Submit your program using the same link you used for Program 1. | Submit your program using the same link you used for Program 1. | ||
+ | <br /> | ||
<br /> | <br /> | ||
+ | =Problem #3= | ||
<br /> | <br /> | ||
+ | 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 [[CSC111 HW 1 2014 | Homework 1]]. However, this time you will be using the '''%-operator''' for formatting output strings. | ||
+ | |||
+ | Feel free to review the solution programs posted for Homework 1 as inspiration for this problem. Programming is also learning from reading code written by others. You may want to use the solution program as a starting point rather than your original program. Feel free to decide how to proceed. | ||
+ | |||
+ | <tanbox> | ||
+ | Reminder: the %-operator for formatting outputs is covered in Section 2.5.3 of the textbook. | ||
+ | </tanbox> | ||
+ | |||
+ | Submit your program using the same link as for Problem 1 above. | ||
<br /> | <br /> | ||
− | |||
<br /> | <br /> | ||
+ | |||
+ | =Additional Important Information= | ||
<br /> | <br /> | ||
− | + | 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: | ||
<br /> | <br /> | ||
− | |||
<br /> | <br /> | ||
+ | |||
+ | <source lang="python"> | ||
+ | # 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() | ||
+ | |||
+ | |||
+ | </source> | ||
+ | |||
<br /> | <br /> | ||
<br /> | <br /> |
Latest revision as of 12:37, 15 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 Friday evening, 2/14/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.76 +------------------------------------+ | Total amount withdrawn: $ 200.76 | | | | 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. Your 2-letter Id can be found here.
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.
Feel free to review the solution programs posted for Homework 1 as inspiration for this problem. Programming is also learning from reading code written by others. You may want to use the solution program as a starting point rather than your original program. Feel free to decide how to proceed.
Reminder: 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()