I gave this problem to return the change back from an input. However, this code a few students wrote produces in correct results. It seems to have an "off by one error". This is for a C exam I gave. I cannot see the error- it has been a long day. Comments would help
Code:
#include <stdio.h>
void change (float total, int *dlrs, int *quart, int *nick,
int *hlfdoll, int *dimes, int *cents);
int main()
{
float total;
int dollar, quarter, nickel, dime, cent, halfdollar = 0;
printf("Please enter an amount in change:\n");
scanf("%f",&total);
change(total, &dollar,&quarter,&nickel,&halfdollar,&dime,¢);
printf("The breakdown for $%.2f is as follows: \n\n", total);
printf ("Dollars %d\n", dollar);
printf ("Half Dollars %d\n", halfdollar);
printf ("Quarters %d\n", quarter);
printf ("Dimes %d\n", dime);
printf ("Nickels %d\n", nickel);
printf ("Pennies %d\n", cent);
return 0;
}
void change (float total, int *dlrs, int *quart, int *nick,
int *hlfdoll, int *dimes, int *cents)
{
int calc;
calc = (int)(total * 100.0);
*dlrs = calc / 100;
calc -= (*dlrs * 100);
*hlfdoll = calc / 50;
calc -= (*hlfdoll * 50);
*quart = calc / 25;
calc -= (*quart * 25);
*dimes = calc / 10;
calc -= (*dimes * 10);
*nick = calc / 5;
calc -= (*nick * 5);
*cents = calc;
return;
}