Okay, I have to make a program using a function with the parameters below in order to allow someone to enter an ammount of money, and then calculate which coins could make up that ammount of money.
Code:
void change(int *nC, double vC, double *amt)
I've wrote the program, which is below here, but for some reason at certain values, such as 3 cents, the pennies are off by 1 or 2. I have a feeling it has something to do with the inacuracy of the floating point numbers and the >=, but no matter what I've done to get around that the results don't change. I tried setting it to run while the amt > 0 and then having the first thing in the while loop being an if statment saying that if amt < vC return; but that produced the same results.
Any information on where I've gone wrong would be great, thank you.
Code:
#include <math.h>
#include <stdio.h>
void change(int *nC, double vC, double *amt);
void main()
{
double z;
int nH,nQ,nD,nN,nP;
double vH = .50, vQ = .25, vD = .10, vN = .05, vP = .01;
printf("Enter an ammount of money: ");
scanf("%lf",&z);
while(z > 0)
{
change(&nH,vH,&z);
change(&nQ,vQ,&z);
change(&nD,vD,&z);
change(&nN,vN,&z);
change(&nP,vP,&z);
printf("Number of Half Dollars: %d\nNumber of Quarters: %d\nNumber of Dimes: %d\nNumber of Nickels: %d\nNumber of Pennies: %d\n",nH,nQ,nD,nN,nP);
printf("Enter an ammount of money: ");
scanf("%lf",&z);
}
}
void change(int *nC, double vC, double *amt)
{
*nC = 0;
while(*amt >= vC)
{
*amt = *amt - vC;
*nC = *nC + 1;
}
}