Help! Program To Find The Least amount of coins with a twist

This is a discussion on Help! Program To Find The Least amount of coins with a twist within the C Programming forums, part of the General Programming Boards category; I can't seem to figure out how to get this program to work with a trick coin of a $1.31 ...

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

    Question Help! Program To Find The Least amount of coins with a twist

    I can't seem to figure out how to get this program to work with a trick coin of a $1.31 to 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[1] = 2.00; /* $2.00 */
        coins[2] = 1.31; /* $1.31 */
        coins[3] = 1.00; /* $1.00 */
        coins[4] = 0.25; /* $0.25 */
        coins[5] = 0.10; /* $0.10 */
        coins[6] = 0.05; /* $0.05 */
        coins[7] = 0.01; /* $0.01 */
        
        
        /* Set Counters For Each Type Of Coin */
        
        for (x = 1; x <= 7; 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 = 1; y <= 7; 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;
    }
    Please Help!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,544
    > coins[7]
    This has 7 elements, in index 0 to index 6

    > coins[7] = 0.01;
    This is outside the array, so all bets are off as to what happens next (see the sig)

    > for (y = 1; y <= 7; y++)
    Again, stepping off the end of the array
    for (y = 0; y < 7; y++)
    accesses the array
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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

    Ok, But What about trick coin!

    New code
    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;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 01-11-2008, 08:34 AM
  2. Replies: 4
    Last Post: 08-15-2002, 11:35 AM
  3. Won't Return pointer, i can't find why...
    By ss3x in forum C++ Programming
    Replies: 2
    Last Post: 02-28-2002, 07:50 PM
  4. How to find the amount of memory
    By Prakash in forum C Programming
    Replies: 1
    Last Post: 09-18-2001, 11:12 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21