Thread: Can't Get This Program To Work Properly

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

    Exclamation Can't Get This Program To Work Properly

    I need to make a program to find the leas amount of coins but with a twist, there is a coin called a joinie that was added worth a $1.31 I don't know ow to get it run properly.


    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 */
        
        printf ("\n");
        
        for (y = 1; y > 7; y++)
        {
            if (moneyfloat >= coins[y])
            {
                           numCoins[y] = (int) (moneyfloat / coins[y]);
                           moneyfloat = moneyfloat - (numCoins[y] * coins[y]);
                           sum = sum + numCoins[y];
            }    
            printf ("$%f coin:  %d - $%f \n",coins[y], numCoins[y],(numCoins[y] * coins[y]));                 
        }
    
        
        /* Close Program (Repeat Twice Because Once Dosn't Work Correctly) */
          
        getchar();
        getchar();
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    43
    Here's an issue:
    Code:
        for (y = 1; y > 7; y++)
    Should be <= I believe

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    That's wrong too. It should be:
    Code:
    for (y = 0; y < 7; y++)
    You are constantly reading/writing past the buffer in your code. coins[7] is invalid since there are only 7 total indexes. That means you can have coins[0 - 6].

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    8
    No what CrazedBrit Said works, but I still can't get least amount of coins with the trick coin in there and i don't know how to make it work.

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    43
    Nono, bithub is right, I was wrong. When you declare an array...ex. array[7] you get 7 chunks of memory. But that memory starts at 0, so its really 0-6. So you need to change that line to the one that he stated, and change all your declarations to start at 0 and end at 6.

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    8
    k, but what about the trick coin how do i make it work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program doesnt work properly.
    By +Azazel+ in forum C Programming
    Replies: 4
    Last Post: 10-12-2007, 06:57 AM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  3. Why does this program not work on my laptop?!
    By Eddie K in forum C Programming
    Replies: 1
    Last Post: 03-11-2006, 04:34 PM
  4. program from book wont work
    By cemock in forum C Programming
    Replies: 2
    Last Post: 03-06-2003, 09:58 AM
  5. how do i get this program to work, anyone, anyone?
    By correlcj in forum C Programming
    Replies: 5
    Last Post: 07-04-2002, 10:28 PM