Thread: HELP! Please

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    8

    HELP! Please

    I need help and finishing this least amout of coins program but it has a twist were there is a coin on a value of $1.31 and i can't seem to get it to get the least amout of coins. For example $4.63 comes up as 2 twonies, 2 quarters, 1 dime, 3 pennies. But it should be 1 twonie, 2 joinies and 1 pennie because that uses the least amount of coins but i don't know what to do to make it work.

    Code:
    #include <stdio.h>
    
    int main()
    {
        /* Declaring variables */
        
        float moneyfloat,coins[7]; 
          /* Money Value From User, Values For Each Type Of Coin */
        
        int x,y,numCoins[7];
          /* Counter, Counter, Number Of Each Type Of Coin Counters */
        
        int sum = 0;
          /* Sum Counter that is set to a value of 0 */
          
          
        /* Set Values For Each Type Of Coin */
              
        coins[0] = 2.00; /* $2.00 */
        coins[1] = 1.31; /* $1.31 */
        coins[2] = 1.00; /* $1.00 */
        coins[3] = 0.25; /* $0.25 */
        coins[4] = 0.10; /* $0.10 */
        coins[5] = 0.05; /* $0.05 */
        coins[6] = 0.01; /* $0.01 */
        
        
        /* Set Counters For Each Type Of Coin */
        
        for (x = 0; x <= 6; x++)
        {
            numCoins[x] = 0;
        }
          /* $2.00, $1.31, $1.00, $0.25, $0.10, $0.05, $0.01 */
          
          
        /* Print Title */
        
        printf ("\nSmallest Combination of Coins Finder\n\n");
        
        
        /* Ask User For Input */
        
        printf ("Enter An Amount of Money: $");
        
        
        /* Get User Input */
        
        scanf ("%f", &moneyfloat); /* Gets Users Input Of Amount of Money */
        
        
        /* New Line */
        
        printf ("\n");
        
        
        /* Sequence To Calculate The Amount Of Coins Needed From The Value Entered */
        
        for (y = 0; y <= 6; y++)
        {
            if (moneyfloat >= coins[y]) 
              /* Money Is More Than Or Equal To Coin Value */
            {
                           numCoins[y] = (int) (moneyfloat / coins[y]);
                             /* Finds number Of Coins */
                           
                           moneyfloat = moneyfloat - (numCoins[y] * coins[y]);
                             /* Takes The Value For The Type of Coins from the Money */
                             
                           
                           sum = sum + numCoins[y];
                             /* Adds amount of coins to sum */
            }    
            printf ("$%f coin:  %d - $%f \n",coins[y], numCoins[y],(numCoins[y] * coins[y]));                 
              /* Prints Type Of Coin, How Many, and Value */
        }
        
        
        /* Print Sum */
        
        printf ("-----------------------------\n");
        printf ("SUM: %d\n",sum);
        
        
        /* Close Program (Repeat Twice Because Once Dosn't Work Correctly) */
          
        getchar();
        getchar();
        return 0;
    }

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Surprise! A greedy algorithm won't work here. The only way I can think of solving this in general is solving two cases and comparing them, but I'm sure someone else has working on this before. You might try a forum search.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    8

    Exclamation I've looked

    But I can't find out how to do this program to work properly

Popular pages Recent additions subscribe to a feed