1

I have a question. When I try to run my code with some inputs it shows correct output (except some inputs I’ll tell you later) like when I run

~cs50/pset1/greedy

But when I run

check50

it shows my output is wrong. Can anyone help me please.

Ok Friends I want help so i m pasting a half of my code which includes round() function as well. Please look into this and tell me what is a bug.

Half Part of my Code For Pset1 Greedy.c

// round function to convert user input to integer value

    int uInput_Int = round(uInput);

    // checks and correct the round value  

    if(uInput_Int > uInput)
    {
        uInput_Int = uInput_Int - 1;
    }

    // convert values after decimal point into integer value       

    int point_value = (uInput - uInput_Int)*100;

    // this for loop count coins for left side value of decimal point    

    for(int i = 0; i < uInput_Int; i++)
    {
        counter = counter+4;
    } 

This is half of my code before this line my code checks input from user.Apart form this bunch of lines the rest of code code deal with right hand side value of decimal.

1
  • 1
    At the bottom of check50's output is a link to a sandbox. Go to that link and expand each thing that's red and see if you can figure out why. Maybe you are printing extra things it's not expecting or not enough things. Feel free to paste that link in your question. Check out this question: cs50.stackexchange.com/questions/13993/…
    – Sam Gerber
    Oct 4 '15 at 15:44
1

Let's say that the user gives 5.6 as input. I guess you firstly assign it to uInput something like so:

float uInput = GetFloat();

but as described in the specification, due to floating point imprecision, it's not stored as 5.6 but as 5.59999990463256835937500000000000000000000000000000 instead (in 50 decimals)

Now, on you first line, you have:

int uInput_Int = round(uInput);

the round() of 5.59999990463256835937500000000000000000000000000000 is 6, so uInput_Int is 6.

Then you have:

if(uInput_Int > uInput)
{
    uInput_Int = uInput_Int - 1;
}

The condition is true, so now uInput_Int becomes 5. (why are you doing this exactly?)

Then

int point_value = (uInput - uInput_Int)*100;

Now uInput - uInput_Int is equal to 5.59999990463256835937500000000000000000000000000000 - 5.0 and that's equal to 0.59999990463256835937500000000000000000000000000000. Then you multiply the result by 100 and you get 59.999990463256835937500000000000000000000000000000.

By now you should have realized that you are way off target from the starting value.

My advice to you is re-read the specification to understand exactly what is asked from you and start from the beginning. Your pseudocode should be like so:

  1. read the input in dollars
  2. multiply by 100 to make them cents
  3. round() the result to make it integer
  4. find the number of coins needed using the integer cents.

If this answers your question please accept it by clicking the gray check-mark to the left, so that it becomes green. You can also vote it up by pressing the up arrow above the check-mark. And don't forget to keep coding!

You must log in to answer this question.

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