I've wrote the following which passes check50 and operates as intended:

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


int cents = round(change * 100);


I've looked at the round reference here and it references a "double round". Everything seems to be in order, but I implemented neither the double round nor the "%i.55" as indicated in the Hints section here.

I understand the need to round the float and assign the variable then as an integer. But, I am only guessing that the double round has to do with currency formatting (i.e., two decimal places) and that the "%i.55" bits have to do with preventing runtime errors if the user inputs outrageously high figures. Or am I completely off? As I've stated it passes the automated check50 but I'd like to have a firm grasp on this before submitting and moving on in the course. Thanks in advance.


double is one of the types in C, like int and char. It's a double-precision floating point number. So

#include <math.h>
double round(double x);

means that the function takes a double and returns a double. You can pass it a float and it will handle that.

So in your usage:

int cents = round(change * 100);

You are rounding the value of change * 100 which then gets stored in cents. Because you've declared cents as an int, the value is cast (ie, changed) to an integer (ie, it is truncated to a whole number). The end result is that, for example, if change is 4.2, then cents will be 420.

The example with "%i.55" as a format specifier was to show how to print a float to 55 decimal places. In greedy, you aren't printing your floats, but instead just the number of coins, which will have no decimal places. This is why you use simply "%i".

You must log in to answer this question.

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