Thread: Check code out-tired today

  1. #1
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Wink 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;
    }
    Mr. C: Author and Instructor

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    What are the incorrect results... can you give example of input and output that is wrong.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    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.
    Last edited by Mister C; 11-23-2002 at 09:23 PM.
    Mr. C: Author and Instructor

  4. #4
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    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.

  5. #5
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    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.
    Mr. C: Author and Instructor

  6. #6
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    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.

  7. #7
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Talking

    Thanks guys! Got there programs to work correctly. Now how much do I count off.......
    Mr. C: Author and Instructor

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MFC check box question
    By MyglyMP2 in forum Windows Programming
    Replies: 2
    Last Post: 03-09-2009, 05:47 PM
  2. HELP!!!!emergency Problem~expert please help
    By unknowppl in forum C++ Programming
    Replies: 9
    Last Post: 08-21-2008, 06:41 PM
  3. << !! Posting Code? Read this First !! >>
    By kermi3 in forum C# Programming
    Replies: 0
    Last Post: 10-14-2002, 01:26 PM
  4. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 12:51 PM
  5. Check my code Please
    By AdioKIP in forum C++ Programming
    Replies: 1
    Last Post: 03-12-2002, 08:52 PM