Thread: C Assign Help: Find the least amount of coins required to make a value with a twist

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    2

    C Assign Help: Find the least amount of coins required to make a value with a twist

    Hey guys, im new to the forums, and sort of new to programming in c. Im currently in my second semester of college so I already have some programming knowledge. Our assignment is this:

    Write a program in C that will allow a user to enter an amount of money, then display the least number of coins and the value of each coin required to make up that amount. The twist to this program is the introduction of a new coin the jonnie (value = $1.26). Therefore, the coins available for your calculations are: Twonies, Loonies, Jonnies, Quarters, Dimes, Nickels and Pennies. You can assume the amount of money user enters will be less than $100.00.

    User enters $4.53. Your output should be:
    The least number of coins required to make up $4.53 is 4. The coins needed are:

    1 Toonie $2.00
    2 Jonnies $2.52
    1 Penny $0.01
    $4.53

    I'm wondering if i'm close. I'm stuck on what to do for the output. This is what i have so far. Any input on making this shorter is greatly appreciated. We can only use #include <stdio.h>

    [tag]
    Code:
    
    
    Code:
    //Start
    
    
    #include <stdio.h>
    
    
    int main()
    {
        //Get user input for amount of money
        
        //Input
        
        int i; 
        
        //Store input from user 
        
        printf("Please enter an amount to find the least number of coins to\nmake up your value: "); 
        scanf("%.2d",i);
           
            //Coin values avail for use 
        
            int t; 
            int j;
            int l;    
            int q; 
            int d;
            int n; 
            int p;
            
            int total_coins;
            
                //Calc the least amount of coins for an input < $100.00
        
                for (i = 0; i < 100;)
                {
          
                    if (i >= 2)
                    {
                        t = i/2;
                        j = i % t / 1.26;
                        l = i % j / 1;
                        q = i % l / 0.25;
                        d = i % q / 0.1;
                        n = i % d / 0.05;
                        p = i % n / 0.01;
                        
                        total_coins = t + j + l + q + d + n + p;
                        
                        getchar();
                    }
                    else if ((i < 2) && (i >= 1.26)) 
                    {
                        j = i % t / 1.26;
                        l = i % j / 1;
                        q = i % l / 0.25;
                        d = i % q / 0.1;
                        n = i % d / 0.05;
                        p = i % n / 0.01; 
                        
                        total_coins = j + l + q + d + n + p;              
                        getchar();
                    }
                    else if ((i < 1.26) && (i >= 1.00))
                    {
                    
                        l = i % j / 1;
                        q = i % l / 0.25;
                        d = i % q / 0.1;
                        n = i % d / 0.05;
                        p = i % n / 0.01;  
                        
                        total_coins = l + q + d + n + p;
                        getchar();
            
                    }
                    else if ((i < 1.00) && (i >= 0.25))
                    {
                    
                        q = i % l / 0.25;
                        d = i % q / 0.1;
                        n = i % d / 0.05;
                        p = i % n / 0.01;
                        
                        total_coins = q + d + n + p;
                        getchar();
            
            
                    }
                    else if (( i < 0.25) && (i >= 0.10))
                    {
                        d = i % q / 0.1;
                        n = i % d / 0.05;
                        p = i % n / 0.01;
                        
                        total_coins = d + n + p;
                        getchar();
            
                    }
                    else if ((i < 0.10) && (i >= 0.05))
                    {
                        n = i % d / 0.05;
                        p = i % n / 0.01;
                        
                        total_coins = n + p;
                        getchar();
            
                    }
                    else if ((i < 0.05) && (i >= 0.01))
                    {
                        
                        total_coins = i / 0.01;
                        getchar();
                                                         
                    }
        
            }                            
                    return 0;
    }
    [/tag]

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    I'd convert dollar and cent to a number of cents. Say $1.32 would be read as two integers (1 and 32 with a decimal point between them), and then combined into a result of 132 cents.

    Then you can just use the modulo operator, with everything done with integral values (no mucking around with floating point - which is error prone for handling money - at all).

    Then just use a sorted array with the value of each type of coin, and allocate largest coins first. I certainly wouldn't duplicate code the way you have.

    Since a "jonnie" is not a multiple of other denominations (but it is a sum) you might try some tricks involving removing jonnies and/or larger denominations from consideration, and comparing the results you get.
    Last edited by grumpy; 03-08-2014 at 11:17 PM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Mar 2014
    Posts
    2
    Thanks so much! Definately makes more sense to me now.. I didn't think about converting the cents part. Just one question how do you sort the array for it though? I'm just starting on arrays in class so i'm somewhat familiar with them

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    You can always initialize it sorted from the largest denomination to the least. Your book will show you what array initialization looks like.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do you find the amount of space in a char array?
    By BatchProgrammer in forum C Programming
    Replies: 11
    Last Post: 05-15-2013, 01:21 PM
  2. Replies: 2
    Last Post: 10-26-2011, 11:18 AM
  3. Replies: 2
    Last Post: 03-15-2006, 08:14 AM
  4. How to find the amount of memory
    By Prakash in forum C Programming
    Replies: 1
    Last Post: 09-18-2001, 11:12 AM
  5. Replies: 2
    Last Post: 09-04-2001, 02:12 PM