I am close to finishing the greedy exercise, but seems there is something funky going on with final value. Currently, here is the pseudo code I am using:

  • float ask
  • int count
  • do-while loop as long as ask <=0
  • create int c
  • c = roundf(ask*100); //I saw this somewhere else, I guess it converts ask float into int
  • while loop for c / 25 >=1, then I add 1 to count, and subtract 25 from c value
  • I also did this while loop for 10, 5, and 1
  • At end, when I compile everything seems fine. But then, unless I enter an amount like 25 which gives me weird values back

Any bugs you can easily identify?

  • What type of weird value? – Vibhaas Apr 16 '16 at 3:35

basic debugging hint: printf the values of your variable in important places (before/after a change)


1.Do-while loops as long as ask <0 is okay. 0 is a non-negative number. 2.Still not very sure about how you write the while loop. My condition is a bit different, as c >= 25, and it works out fine. Might need more information about the loops.


Being able to see your current code especially in the while loop would certainly help.

Anyway, here are the potential sources of your bugs:

First, during the do-while loop, you are telling the program not to accept if the value entered is zero or below. You should avoid it as 0 is considered a non-negative value. It should be:

while (ask < 0)

Second, using while-loop could probably do it, but I think it would be a bit confusing. We would need to see you're code so we can help debug it. But in my case, I hard coded it using if statements.

I wrote this a few weeks ago. You can check the bottom part where I had more documentary on the Greedy Problem:

My Experience with CS50’s Week 0 and 1 and Its Problem Sets

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .