0

I've coded this problem set and it works completely fine on my machine printing all desired output.

0.41 output: 4 and so on.

However, in gradebook it says it did not get the required output for given inputs. why so? I'm on windows machine.

/**
 *
 * greedy.c
 *
 * Ashish Patel
 * ashish.xxxxxxxxxxxxxx@gmail.com
 *
 * CS50 problem set 1
 * greedy algorithms finds minimum number of coins to make change
 *
 */

 #include <stdio.h>
 #include <math.h>
 #include "cs50.h"

 int main(void)
 {
     float n;
     int count = 0, amount;

     // prompts user for amount owed and validates input
     do
     {
         printf("How much change is owed?\n");
         n = GetFloat();
     }
     while (n < 0);


    // convert input into cents
     n *= 100.0;
     amount = (int) round(n);

     // while quarter can be used
     while (amount >= 25)
     {
         count++;
         amount -= 25;
     }

     // while dimes can be used
     while (amount >= 10)
     {
         count++;
         amount -= 10;
     }

     // while nickels can be used
     while (amount >= 5)
     {
         count++;
         amount -= 5;
     }

     // while pennies can be used
     while (amount >= 1)
     {
         count++;
         amount -= 1;
     }

     // prints the minimum number of coins for change
     printf("%d", count);

     return 0;
 }
1
  • hi! May I ask why did you put 'int count = 0, amount;' together?
    – UgurS
    Dec 8 '17 at 21:25
6

Add a new line at the end of your printf string like that:

printf("%d\n", count);
5
  • Ah so I lost all my grades for this tiny mistake? :/ I fixed it. Thanks a lot, Chris. Jul 10 '15 at 17:32
  • Mark the answer as correct if it helped you. Also you didn't lose your grades. You just have to resubmit the corrected code and you will get the full grade!
    – ChrisG
    Jul 10 '15 at 17:33
  • oh sure thanks, done! Jul 10 '15 at 17:36
  • amount = (int) round(n); Whats the exact purpose of this line? Could you help me out in understanding this? Thank you! :) Dec 14 '16 at 12:31
  • n is a float. By calling round() on that float we "round" it to its closest integer (for example 3.4 becomes 3.0 and 3.5 becomes 4). But the return value of round() is still a float, so we cast it to an int using (int) in front of the call to round(). We then assign the integer result to the variable amount.
    – ChrisG
    Dec 15 '16 at 21:18

You must log in to answer this question.

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