I thought I wrote code that would work for greedy in pset1, but every time I run the program, it either outputs 0 or some super large number. What is the problem?


int main(void)
    float change = -1;
    float c2;
    int c3;
    int coins = 0;
    int q;
    int d;
    int n;
    int p;
    do {
        printf("How much change is due?\n");
        change = GetFloat();
    while (change <= 0);
    c2 = change*100;
    c3 = roundf(c2);

    while (c3 >= 25)
        q = coins++;
        c3 = c3 - 25;
    while (c3 >= 10 && c3 < 25)
        d = q++;
        c3 = c3 - 10;
    while (c3 >= 05 && c3 < 10)
        n = d++;
        c3 = c3 - 05;
    while (c3 >= 01 && c3 < 5)
        p = n++;
        c3 = c3 - 01;
    printf("the ammount of coins to use for change is: %i\n", p);

1 Answer 1


The code has 3 different errors. First, as you count the coins, you are post-incrementing and assigning the values, as in this: q = coins++;. That means that when you want to add a coin to the count, it is actually assigning the current value in coins to q, and then adding 1 to coins. The ++ should come before the variable to pre-increment ( increment before executing the assignment) instead of post-increment.

The second problem is that the variables q, d, n and p are not initialized to 0, so they contain garbage data. If change is a value that causes a coin denomination to be skipped, (i.e., no dimes), then that coin variable (d, in this case) will not be properly set and the garbage value will be used in processing the next coin denomination.

Third, the code is highly dependent on there being at least one coin of each type. If any coin type is not required (i.e., no nickels), it's going to lose track of whatever count existed before that coin.

The last two problems are a result of trying to cascade from one coin type to the next. Instead, you should use one single variable to count the coins from beginning to end. By doing so, there will no longer be a problem if a coin type isn't needed to make change.

A side note. Even after you fix these issues, it will fail check50. The hidden lesson in this pset is that you need to write your code to exactly the specification - no extra or missing spaces, whitespace, text, etc. In this case, it says to print out the number of coins. Your addition of text, the ammount of coins to use for change is: will cause the failure.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

  • Thank you for your help! I had a few more errors but have since solved them! Jun 7, 2016 at 21:23
  • without seeing code, can't help.
    – Cliff B
    Jun 7, 2016 at 21:25
  • I see what you're doing here, but I don't think you need all those variables. You could use a single float, float c2 = 100 * GetFloat(); to make things tidier. More importantly, a single variable containing the running total of coins needed would be sufficient, rather than coins/q/d/n/p. Jun 8, 2016 at 3:27

You must log in to answer this question.

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