Problems with int, float, and for loops in greedy

I am having a hard time with greedy. I think this first part is OK; I already checked it with check50. But in this second part I think I was kind of guessing. I put the first two lines just to show where money is coming from:

int money;
money = GetFloat();

int change = money*100;
int quarter, dime, nickel, penny;
int c1 = 0;
for (change =0; change >= 25; change--)
{
quarter = change - 25;
c1++;
}

I made a similar loop for each kind of coin and wrote this to print out the result.

printf("%d\n",c1+c2+c3+c4);

The only output I am getting from this sum is 0.

Can I make an int out of a float by multiplying it by 100?

Is the variable c1 updated each time the for loop runs?

To avoid problems, you can make it into a while loop. Also, assign values to your quarter, dime, nickel and penny.

int quarter=25,dime=10,nickel=5,penny=1;

while(change>=quarter)
{
c1++;
change-=quarter;
}

//Other loops

//Print count
• Great! I worked on those while loops and a problem of rounding the float money. pset1 solved. thanks.
– msk
Jul 2 '14 at 19:24
• Glad I could help :)
– kray
Jul 2 '14 at 19:27

In your loop you're overwritting the variable change in it's declaration for (change =0; change >= 25; change--). First you tell that change = 0 so you are deleting your entry, also, each time the loop runs it should subtract 1 (change--) from the variable (apart to the 25 subtraction inside the loop... so it should subtract 26).

Even so, the loop declaration is also wrong, because you're asking for a countdown i-- while your loop has to run from 0 to 25).

By the way, money*100 can give you problems due the float imprecision, you should round that result.