There is a question in "Problem Solving and Program Design in C" book.I wrote the code, but loop isn't terminating.
#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);
}
Thank you, I solved problem.The proper code is
#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);
}
It's not a good idea to treat money as a floating-point quantity; floating point numbers have limited precision and (worse) limits in which decimal fractions they can represent.
If you described your problem better, it would be easier to give more specific help.
In while(coin_change>0)
comparing doubles to exactly 0 is never a good idea because floating point numbers have an inexact representation. Set some tolerance, like 0.01
and check to see if coin change > 0.01
.
In your change() function, you should just write directly to the variables, EG:
if(coin_change >= 0.25){
*quarters = *quarters ++;
coin_change = coin_change - q*0.25;
}
Your current method will add many unnecessary coins. Think about it:
`q = 0; //...
if(coin_change >= 0.25){ //True, enter conditional
q++; //q == 1
*quarters = *quarters + q; //*quarters = 1
coin_change = coin_change - q*0.25; //Coin change == .75
}
//...
if(coin_change >= 0.25){ //True, enter conditional
q++; //q == 2
*quarters = *quarters + q; //*quarters = 3 ( 1 + 2 )
coin_change = coin_change - q*0.25; //Coin change == .50
}
//...
if(coin_change >= 0.25){ //True, enter conditional
q++; //q == 3
*quarters = *quarters + q; //*quarters = 6 ( 3 + 3 )
coin_change = coin_change - q*0.25; //Coin change = .25
}
`
You should also use an integer for coin change, just multiply the value by 100 and keep track of pennies, so $1 = 100.
also, you could use 4 while loops for a more clear line of command:
while(coin_change >= 0.25){
*quarters = *quarters ++;
coin_change = coin_change - q*0.25;
}
精彩评论