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);
}