Andrewoca
Andrewoca

Reputation: 79

Why does my code not return the required output after prompting user

I am making a simple Algo that returns the minimum number of coins needed to give back in change when a $ value is typed in, using only 25c, 10c, 5c and 1c coins.

eg Amount owed: 0.31 coins: 3

please see the code below, it prompts the user correctly but does return any number when given an input.

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

int main(void)

{
    //Declaring the variables
   float change_owed;
   int quarter, dime, nickel, penny;
    //Defining types of coins that can be given as change.
    quarter = 25;
    dime = 10;
    nickel = 5;
    penny = 1;

    do
    {
    //Defining function and storing in variable change
        change_owed = get_float("Change: ");
    }  
    //Repeat prompt while input is negative  
    while (change_owed <= 0);
    //rounding the numbers so we can divide
    int amount = round(change_owed * 100);

    ///Deciding if to use a Quarter
    //Defining Quarter counter
    int count_quarter=0;
    while (amount >= quarter)
    {   
        //Counting number of quarters used
        count_quarter++;
        //Decreasing amount owed by a quarter
        return amount - quarter;
    }
    //Deciding whether to use a dime
    //Defining dime counter
    int count_dime=0;
    while (amount >= dime)
    {
        //Counting number of dimes used
        count_dime++;
        //Decreasing amount owed by a dime
        return amount - dime;
    }
    int count_nickel=0;
    while (amount >= nickel)
    {
       //Deciding whether to use a nickel
       //counting number of nickels used 
        count_nickel++;
        //Decreasing amount by a nickel
        return amount - nickel;
    }

    //Deciding whether to use a penny
    //Defining penny counter
    int count_penny=0;
    while (amount >= penny)
    {
       //counting number of pennies used
        count_penny++;
      //Decreasing amount by a penny
      return amount - penny; 
    }

  int total_coins = (count_quarter + count_dime + count_nickel + count_penny);

    printf("%i", total_coins);

}

Upvotes: 0

Views: 136

Answers (1)

jaaq
jaaq

Reputation: 1256

The return statement in C(or any language I can think of) returns a value from a function. Meaning after the return statement no more code of that function is executed, and the next instruction will be the next one after your function. If you return from main, the exit function is called which among other things terminates the process and makes sure the return value of the process is made available.

On bash if you do:

./a.out
echo $?

you can see the return value of your C program.

What you want is keep adding the biggest possible coin as long as possible(greedy algorithm), then switch to the next smaller coin.

The good way, use ints and calculate everything in cents.

#include "stdio.h"

#define NUM_COIN_TYPES 4

int main(void)
{
        int coins[NUM_COIN_TYPES] = {25, 10, 5, 1};
        float change_owed_float = 13.37; //insert your read from commandline code here
        int change_owed = (int)(change_owed_float * 100.f);
        int num_coins = 0;

        for(int ctr = 0; ctr < NUM_COIN_TYPES && change_owed > 0; ++ctr)
        {
                while(change_owed - coins[ctr] >= 0)
                {
                        change_owed -= coins[ctr];
                        ++num_coins;
                }
        }

        printf("Number of coins required: %d\nChange owed: %d\n", num_coins, change_owed);
        return 0; //could be void main and no return
}

The float way that can have weirdness happen:

#include "stdio.h"

#define NUM_COIN_TYPES 4

int main(void)
{
        float coins[NUM_COIN_TYPES] = {.25,.10,.05, .01};
        float change_owed = 13.37; //insert your read from commandline code here
        int num_coins = 0;

        for(int ctr = 0; ctr < NUM_COIN_TYPES && change_owed > 0.0; ++ctr)
        {
                while(change_owed - coins[ctr] >= -.009) //floats are weird
                {
                   change_owed -= coins[ctr];
                   ++num_coins;
                }
        }

        if(change_owed > -.01 && change_owed <= 0.0)
        {
                //sanity check
                printf("It works!\n");
                change_owed = 0;
        }

        printf("Number of coins required: %d\nChange owed: %f\n", num_coins, change_owed);
        return 0; //could be void main and no return
}

EDIT: Probably the easiest way to avoid the weirdness of floating point arithmetic, is to just use ints after multiplying everything by 100.

HTH

Upvotes: 1

Related Questions