CPSC150A
Scientific Computing

Activity 4

Reassignment and Updating

Average

Write a python program that reads 4 numbers from the user and prints the average of the numbers. Note, this program could be written without reusing or updating any variables, but for the sake of practice, write the program using only one variable to compute the sum.

Example

Enter a number:
                                                            1.0
Enter a number:
							    2.0
Enter a number:
							    3.0
Enter a number:
							    4.0
The average is 2.5

Change

There are many people that would call me a lazy person. I don't find that offensive, but I think it's not a valid description. It's not that I'm lazy, I just try to be as efficient as possible. For example, in college a group of friends and I deduced the fewest amount of coins to carry that would produce any arbitrary amount of change. While a computation like that is still a little too complex, figuring out the fewest amount of coins for a fixed amount of change is possible.

Details

Create a Python program that reads a number of cents from the user and prints the fewest number of coins, using pennies, nickles, dimes and quarters, needed to make change. You can assume that the number of cents entered by the user will be between 0 and 100.

Example

Enter cents:
                                                             92
92 cents is:
3 quarter(s)
1 dime(s)
1 nickle(s)
2 penny(s)
  • You can use integer division (//) to determine how many times a coin goes into the change amount evenly. For example, 92 // 25 is 3, so 3 quaters goes into 92 cents evenly.
  • You can use the mod (%) operator to determine the remainder of an integer division operator. Use mod with variable updating to determine the number of cents that are left after coins are removed. For example, 92 % 25 is 17, so there are 17 cents left after taking 3 quarters out of 92 cents.
  • Start with the largest coin first (quarters) and work your way down. This is known as a greedy approach, and is very common in Computer Science.

Challenge

Modify the change program so that it works for dollars as well. The program should read an amount of money as a float and print the fewest number of bills and coins that is equivalent to the entered amount.

Example

Enter cents:
                                                         237.92
237.92 dollars is:
2 hundred(s)
0 fifty(s)
1 twenty(s)
1 ten(s)							 
1 five(s)
2 one(s)							 
3 quarter(s)
1 dime(s)
1 nickle(s)
2 penny(s)