HARVARD'S CS50 PROBLEM SET 1: C _Time For Change (Greedy)

11:31:00 AM

After CS50 Week 1
Where to find problem set 1:C?

  • Get user's input in dollars, and make sure it's a valid positive number
  • Always use the largest coin possible
  • Keep track of coin used
  • Print the final amount of coin 


What is my pseudo-code?
do
    get user's input
while (input is not a positive number)
round (input to cents, and get rid of the excess due to the floating point imperfection)

use while loop to write 

while (quarters can be used)
    increase amount
    amount decrease by a quarter
while (dimes can be used)
    increase amount
    amount decrease by a dime
while (pennys can be used)
    increase amount
    amount decrease by a penny

print number of coins used


What is my result? & How does it check out by CS50?




















What I learned?
1. write a pseudo-code before start to write codes
2. floating point imperfection (imperfection.c) & use round function to fix the imperfection
roundf (x): x is the number that is going to be round
converts user's input to cents first, and then use round to get rid of the excess
3. modulo math
% returns the remainder of the division
10%5=0
6%5=1
4. Use ctrl-c to end a forever looping program
5. Maybe this is obvious to a programmer, but it's definately not to a beginner, loops can be nested within one and another. 

You Might Also Like

0 comments

Popular Posts

Like us on Facebook

Flickr Images

Subscribe