Calculating the number of coins in money change I wrote a program that calculates the minimum number of coins required to give a user change.

One concern I have is: When do we initialize the value of a float to be negative or positive? I recently saw 


float userInput = -1.0;



but what about 


float userInput = 1.0;



Are there differences between the two, and when would one be used over the other?

#include &lt;stdio.h&gt;
#include &lt;cs50.h&gt;
#include &lt;math.h&gt;


    int main(void)
{
    float input = -1;
    int z = 0;
    int counter = 0;

    do
    {
        printf("The amount of changed owed(in dollars) is ");
        input = get_float();
    }
    while (input &lt; 0);
    input = input * 100;
    input = round(input);
    z = input;

    while (z &gt;= 25)
    {
        z = z - 25;
        counter++;
    }

    while (z &gt;= 10)
    {
        z = z - 10;
        counter++;
    }

    while (z &gt;= 5)
    {
        z = z - 5;
        counter++;
    }

    while (z &gt;= 1)
    {
        z = z-1;
        counter++;
    }

    printf("The number of minimum coins needed is %d\n", counter);
}
In general we want to initialize a variable directly to the value it needs, rather than to a temporary "invalid" value. This reduces complexity, and helps to prevent the accidental use of the "invalid" value as if it were a real input.

We should also put variable declarations as close to the point of use as practical. (e.g. z could be declared at the point of assignment from input). It's best for variables to only exist in the scope in which they are needed.

Note that the input variable is effectively reassigned 3 times, and used to represent 3 different things in the program. If we split the program into separate stages, this becomes clearer:

int main(void)
{
    float input = -1; 

    // here "input" is invalid - it's just a placeholder.

    // get user input (dollars):
    {
        do
        {
            printf("The amount of changed owed(in dollars) is ");
            input = get_float();
        }
        while (input &lt; 0);
    }

    // here "input" is the amount of dollars as a float

    // convert input to cents:
    {
        input = input * 100;
        input = round(input);
    }

    // here "input" is the number of cents, as a float

    // calculate number of coins for change:
    {
        int z = input; // note we actually want the number of cents as an int...
        int counter = 0;

        ...

        printf("The number of minimum coins needed is %d\n", counter);
    }
}


It's best to avoid reusing variables like this. Any name given to such a variable is inaccurate or very general (e.g. "input"). Also, when changing some part of the program, we have to understand and modify a much larger amount of code than would otherwise be necessary.

Here, we can avoid reusing the variable by splitting the program up using functions, e.g.:

float get_dollar_input()
{
    while (true)
    {
        printf("The amount of change owed (in dollars) is: ");

        const float dollars = get_float(); // variable initialized to actual value :)

        if (dollars &lt; 0)
        {
            printf("Input must not be negative.");
            continue;
        }

        return dollars;
    }
}

int convert_to_cents(float dollars)
{
    return (int)round(dollars * 100);
}

int calculate_minimal_coins(int cents)
{
    int counter = 0;

    // ...

    return counter;
}

int main(void)
{
    const float dollars = get_dollar_input();
    const int cents = convert_to_cents(dollars);
    const int coins = calculate_minimal_coins(cents);

    printf("The minimum number of coins needed is %d\n", coins);
}




When calculating the change, we do a lot of subtraction in a loop. For a large input (e.g. $200,457,298.46), this could take a looooooong time. We can use division to find the count of each coin, and the remainder (modulus) operator to apply the subtraction:

int calculate_minimal_coins(int cents)
{
    const int quarters = cents / 25;
    cents %= 25;

    const int dimes = cents / 10;
    cents %= 10;

    const int nickels = cents / 5;
    cents %= 5;

    const int pennies = cents; /* unnecessary, but explanatory */

    return quarters + dimes + nickels + pennies;
}




One last thing: We should never use floating point variables to represent exact monetary values. It would be better to ask the user for the number of cents as an integer (or perhaps to ask for two integers: one for dollars, and one for cents).