0

My code for PSET1 is below. it works perfectly on my gedit howver the check50 tells me that it hasnt worked for 1.6 -- 23 -- 4.2 but when i input those numbers it works perfectly. please help!!

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


int main(void)
{
    float c;
    int p;
    int numofcoins = 0;
    do{
    printf("how much change you butthole?\n");
    c = GetFloat();
    }while (c < 0);
    c = round(c*100) - (floor(c) *100);
    p = c;
    //printf("%i\n", p);
    while (p >= 25)
    {
        p = p - 25;
        numofcoins++;
    }
    while (p >= 10)
    {
        p = p - 10;
        numofcoins++;
    }
    while (p >= 5)
    {
        p = p - 5;
        numofcoins++;
    }
    while (p >= 1)
    {
        p = p - 1;
        numofcoins++;
    }
    printf("%i\n", numofcoins);
}
// must be float must be non negative
// use round function
1
  • Could you please post the exact output from check50? Thx – KernelRutgers Mar 10 '15 at 6:48
1

Your program returns results, but indeed they are incorrect.

input: 1.6
yield: 3

(takes more than 3 coins to give change for $1.60)

input: 23
yield: 0

(takes more than 0 coins to give change for $23.00)

I believe your calculation for c is incorrect. I'd take a look at that by uncommenting your test output on line 17.

2
  • Hey no i figured it out, i just have to remove my floor function! it works perfectly when i remove it, nothing to do with output test. i had not understood that they wanted coins for values above one dollar. EI I only give back 2 quarters for 10.50 because i assumed the rest would be in bills – Nicky Feller Mar 14 '15 at 5:18
  • $1.60-$1 = $0.60, which the greedy algorithm should yield 2 quarters + 1 dime for the optimal solution. This makes 3 coins. Nicky Feller, I don't understand what you mean about your solution working once you remove the float. Can you please elaborate? – user6564 Mar 14 '15 at 17:57

You must log in to answer this question.

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