Thread: Dispense Change program

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    48

    Dispense Change program

    Hi,
    I need to write a dispense change program.I completed the code,but it has some problem.Please help me to debug the code.

    Code:
    #include <stdio.h>
    #include <math.h>
    void change(double coin_change, int *quarters, int *dimes, int *nickels, int *pennies);
    int main(void)
    {
    	int c_dollars, c_quarters = 0, c_dimes = 0, c_nickels = 0, c_pennies = 0;
    	double a_paid, a_due, m_change, coin_change;
    	printf("Enter the amount paid> ");
    	scanf("%lf", &a_paid);
    	printf("Enter the amount due> ");
    	scanf("%lf", &a_due);
    	m_change = a_paid - a_due;
    	c_dollars = floor(m_change);
    	coin_change = m_change - floor(m_change);
    	// shows coin change
    	printf("\n%f\n", coin_change);
    	change(coin_change, &c_quarters, &c_dimes, &c_nickels, &c_pennies);
    	printf("Change is dollars: %d$, quarters: %d, dimes: %d, nickels: %d,\
    pennies: %d", c_dollars, c_quarters, c_dimes, c_nickels, c_pennies);
    	return(0);
    }
    void change(double coin_change, int *quarters, int *dimes, int *nickels, int *pennies)
    {
    	int q = 0, d = 0, n = 0, p = 0;
    	 do{
    		if(coin_change >= 0.25){
    			q++;
    			*quarters = *quarters + q;
    			coin_change = coin_change - q*0.25;
    		}
    		else if( coin_change >= 0.10){
    			d++;
    			*dimes = *dimes + d;
    			coin_change = coin_change - 0.1;
    		}
    		else if( coin_change  >=  0.05){
    			n++;
    			*nickels = *nickels + n;	
    			coin_change = coin_change - (n*0.05);
    		}
    		else if(coin_change >= 0.01){
    			p++;
    			*pennies = *pennies + p;
    			coin_change = coin_change - (p*0.01);
    		}
    	}while(coin_change>0);
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    1) If you do some searching on the forum you will find this problem has been here several times before.
    2) Exactly what problems are you experiencing, in which parts of your code?

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    48
    The loop can not be terminated.( do-while loop )

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    To avoid possible rounding errors, I would multiply by 100 to get a value of 0 to 99 as input into the function.

    Tim S.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by paranoidgnu View Post
    The loop can not be terminated.( do-while loop )
    Code:
    do 
      {
    
    } while (coin_change > .01)
    There's nothing to do with less than a penny.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    coin_change = coin_change - 0.1 is different than the other sections.
    Also, the calculation for number of coins is flawed. For example, if you expect 3 quarters...
    q = 1
    *quarters = *quarters + q; - will add 1
    q = 2
    *quarters = *quarters + q; - will add 2, total 3
    q = 3
    *quarters = *quarters + q; - will add 3, total 6

    Also, you should work with integer pennies to avoid floating-point roundoff issues.
    Change the coin_change to be int, then coin_change = (int)((m_change - floor(m_change)) * 100 + 0.5)
    In the function work with 25, 10, 5, 1 (whole numbers).
    Last edited by nonoob; 04-18-2011 at 08:46 AM.

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    48
    Quote Originally Posted by nonoob View Post
    coin_change = coin_change - 0.1 is different than the other sections.
    Also, the calculation for number of coins is flawed. For example, if you expect 3 quarters...
    q = 1
    *quarters = *quarters + q; - will add 1
    q = 2
    *quarters = *quarters + q; - will add 2, total 3
    q = 3
    *quarters = *quarters + q; - will add 3, total 6

    Also, you should work with integer pennies to avoid floating-point roundoff issues.
    Change the coin_change to be int, then coin_change = (int)((m_change - floor(m_change)) * 100 + 0.5)
    In the function work with 25, 10, 5, 1 (whole numbers).
    Thanks nonoob,
    The problem has just solved.The right code is here;
    Code:
    #include <stdio.h>
    #include <math.h>
    void change(double coin_change, int *quarters, int *dimes, int *nickels, int *pennies);
    int main(void)
    {
    	int c_dollars, c_quarters = 0, c_dimes = 0, c_nickels = 0, c_pennies = 0;
    	double a_paid, a_due, m_change, coin_change;
    	printf("Enter the amount paid> ");
    	scanf("%lf", &a_paid);
    	printf("Enter the amount due> ");
    	scanf("%lf", &a_due);
    	m_change = a_paid - a_due;
    	c_dollars = floor(m_change);
    	coin_change = (int)((m_change - floor(m_change)) * 100 + 0.5);
    	// shows coin change (int)((m_change - floor(m_change)) * 100 + 0.5)
    	//coin_change = coin_change * 100;
    	printf("\n%f\n", coin_change);
    	
    	change(coin_change, &c_quarters, &c_dimes, &c_nickels, &c_pennies);
    	printf("Change is dollars: %d$, quarters: %d, dimes: %d, nickels: %d,\
    pennies: %d", c_dollars, c_quarters, c_dimes, c_nickels, c_pennies);
    	return(0);
    }
    void change(double coin_change, int *quarters, int *dimes, int *nickels, int *pennies)
    {
    	int q = 1, d = 1, n = 1, p = 1;
    	 do{
    		if(coin_change >= 25){
    			*quarters = *quarters + q;
    			coin_change = coin_change - 25;
    		}
    		else if( coin_change >= 10){
    			*dimes = *dimes + d;
    			coin_change = coin_change - 10;
    		}
    		else if( coin_change  >=  5){
    			*nickels = *nickels + n;	
    			coin_change = coin_change - 5;
    		}
    		else if(coin_change >= 1){
    			*pennies = *pennies + p;
    			coin_change = coin_change - 1;
    		}
    	}while (coin_change >= 1);
    }

  8. #8
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    You can define coin_change to be int, and same with the first parameter of change() to clean things up.
    I hope you understand and can explain how the fractional dollars are converted to integer.

  9. #9
    Registered User
    Join Date
    Apr 2011
    Posts
    48
    Quote Originally Posted by nonoob View Post
    You can define coin_change to be int, and same with the first parameter of change() to clean things up.
    I hope you understand and can explain how the fractional dollars are converted to integer.
    Thanks for your help.
    why we added 0.5 to our fractional dollars

  10. #10
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Yes I figured you should know that. We multiply the fractional dollars part to make it integer cents. Then add 0.5 (1/2 cent) to round it to the nearest penny.
    In case the input is .587 dollars = 59 cents to the nearest cent. Adding 0.5 means the number is rounded up if it exceeds 1/2 cent. Below 1/2 cent it will be chopped.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with change program
    By f4ichick02 in forum C Programming
    Replies: 12
    Last Post: 10-21-2009, 02:49 PM
  2. Weight Change Program - Help
    By battousaih in forum C Programming
    Replies: 14
    Last Post: 10-01-2007, 04:34 PM
  3. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  4. Change of base program
    By cdonlan in forum C Programming
    Replies: 2
    Last Post: 01-17-2005, 04:51 PM
  5. coin change program??
    By kelly in forum C++ Programming
    Replies: 3
    Last Post: 03-24-2002, 08:57 PM