# Check code out-tired today

Printable View

• 11-23-2002
Mister C
Check code out-tired today
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,&cent);         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; }```
• 11-23-2002
Hammer
What are the incorrect results... can you give example of input and output that is wrong.
• 11-23-2002
Mister C
I am not a school now. Try it with another compiler. I am at home an do not have access to one immediately.

Anyway when I run it here is what it does:

Code:

```Please enter an amount in change; 0.45 The breakdown for \$0.45 is as follows: Dollars 0 Half Dollars 0 Quarters 1 Dimes 1 Nickels 1 Pennies 4```
The pennies are of course wrong. I have tried with other values but pennies appears to be only one calculate wrong.
• 11-23-2002
Nick
This could be due to randomizing errors in passing
the float total. Say .45 is expressed in the computer
as .449999999999 and then the student times it by
44.9999999 truncates it to 44. Really just not
being careful.
• 11-23-2002
Mister C
Quote:

This could be due to randomizing errors in passing
the float total. Say .45 is expressed in the computer
as .449999999999 and then the student times it by
44.9999999 truncates it to 44. Really just not
being careful.
I see your point -how would you tell these students to correct it?

1. Use the ceil function() in the cast

or

2. Tell them these floating point calculations are not alway precisely what the want.
• 11-23-2002
Nick
It might be better to code it like

calc = (int)(total * 100.0 + epsilon);
Where epsilon is large enough to take away the discrepriencies.
Another way would to just use fixed base 10 math. So
like for 45.45 you would internally represent that
as 4545. There would be then no floating point calculations.
• 11-23-2002
Mister C
Thanks guys! Got there programs to work correctly. Now how much do I count off.......