Like Tree4Likes

a dime off.

This is a discussion on a dime off. within the C Programming forums, part of the General Programming Boards category; Hey guys- I am a dime off on this program and I can't figure out why. Any suggestions? Thanks. Code: ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    99

    a dime off.

    Hey guys-
    I am a dime off on this program and I can't figure out why. Any suggestions? Thanks.

    Code:
    #include <stdio.h>
    
    
    struct dollarsQuartersDimes
    {
        int dollars,
            quarters,
            dimes;
    };
    
    
    int main(void)
    {
        dollarsQuartersDimes dQD1, sum, dQD2 = {  };
        
        printf("\nEnter the dollars, quarters, dimes in purse 1 : \n");
        
        scanf("%d%d%d", &dQD1.dollars, &dQD1.quarters, &dQD1.dimes);
        
        printf("\nEnter the dollars, quarters, dimes in purse 2 : \n");
        
        scanf("%d%d%d", &dQD2.dollars, &dQD2.quarters, &dQD2.dimes);
        
        sum.dollars = dQD1.dollars + dQD2.dollars;  
        sum.quarters = dQD1.quarters + dQD2.quarters;
        sum.dimes = dQD1.dimes + dQD2.dimes;
        
        while (sum.quarters > 3)
            {
            ++sum.dollars;
            sum.quarters = sum.quarters - 4;    
            }
        
        while (sum.dimes > 9)
            {
            ++sum.dollars;
            sum.dimes = sum.dimes - 10;    
            }
            
        while (sum.dimes > 2)
            {
            ++sum.quarters;
            sum.dimes = sum.dimes - 2.5;    
            }
        
    printf("\n   %d dollars %d quarters %d dimes\n+  %d dollars %d quarters %d dimes\n----------------------------\n   %d dollars %d quarters %d dimes \n", dQD1.dollars, dQD1.quarters, dQD1.dimes, dQD2.dollars, dQD2.quarters, dQD2.dimes, sum.dollars, sum.quarters, sum.dimes);
                
    return 0;
    Attached Images Attached Images  

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,255
    Your last loop (mapping dimes to quarters) is the culprit.
    Code:
        sum.dimes = sum.dimes - 2.5;
    will do the subtraction in floating point, and then round toward zero in storing the result to sum.dimes. So, if sum.dimes is positive, this line will subtract 3.

    You would be better off converting the input into dollars and cents, and then convert the number of cents back to quarters and dimes. (That's one way, anyway). Otherwise you're being affected by the fact that a quarter is not a whole number of dimes.
    stahta01 and rosemary like this.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    Like this? But it still does not give me correct change.
    Code:
    #include <stdio.h>
    
    
    struct dollarsQuartersDimes
    {
    	int dollars,
    	    quarters,
    	    dimes;
    };
    
    
    int main(void)
    {
    	dollarsQuartersDimes dQD1, sum, dQD2 = {  };
    	
    	printf("\nEnter the dollars, quarters, dimes in purse 1 : \n");
    	
    	scanf("%d%d%d", &dQD1.dollars, &dQD1.quarters, &dQD1.dimes);
    	
    	printf("\nEnter the dollars, quarters, dimes in purse 2 : \n");
    	
    	scanf("%d%d%d", &dQD2.dollars, &dQD2.quarters, &dQD2.dimes);
    	
    	sum.dollars = (dQD1.dollars + dQD2.dollars) * 100;  
    	sum.quarters = (dQD1.quarters + dQD2.quarters) * 25;
    	sum.dimes = (dQD1.dimes + dQD2.dimes) * 10;
    	
    	while (sum.quarters > 99)
    		{
    		sum.dollars = sum.dollars + 100;
    		sum.quarters = sum.quarters - 100;	
    		}
    	
    	while (sum.dimes > 90)
    		{
    		sum.dollars = sum.dollars + 100;
    		sum.dimes = sum.dimes - 100;	
    	    }
    		
    	while (sum.dimes > 20)
    		{
    		sum.quarters = sum.quarters + 25;
    		sum.dimes = sum.dimes - 25;		
    		}
    	
    	    sum.dollars = sum.dollars / 100;
    	    sum.quarters = sum.quarters / 100;
    	    sum.dimes = sum.dimes / 100;
    	    
    printf("\n   %d dollars %d quarters %d dimes\n+  %d dollars %d quarters %d dimes\n----------------------------\n   %d dollars %d quarters %d dimes \n", dQD1.dollars, dQD1.quarters, dQD1.dimes, dQD2.dollars, dQD2.quarters, dQD2.dimes, sum.dollars, sum.quarters, sum.dimes);
    			
    return 0;
    }

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    can anyone help? thanks.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your sum of <coin> is really just pennies now. Change your while loops, accordingly. After you run through the quarters in the while loop, you can't say sum quarters += 25. That's for pennies, and you have already changed pennies back into quarters: so you need sum quarters++;

    Same for all the rest of the loops. If you're lost with this, grab some coins with a bit of each type, and work it through by hand AS IF you were the program. Talk to yourself, about what you're doing, as you go through it. The light bulb will come on shortly.

    Then look at your while(loopTest), loopTest. Those aren't quite right, are they? If I have 21 cents, I don't have a quarter, see?
    Last edited by Adak; 10-22-2012 at 07:59 PM.

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    Quote Originally Posted by Adak View Post
    If I have 21 cents, I don't have a quarter, see?
    wouldn't it be impossible to have 21 cents? so, if it's above 20 cents, it has to be 30 cents, right?

  7. #7
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    ok. here's my changed code.
    Code:
    #include <stdio.h>
    
    
    struct dollarsQuartersDimes
    {
    	int dollars,
    	    quarters,
    	    dimes;
    };
    
    
    int main(void)
    {
    	dollarsQuartersDimes dQD1, sum, dQD2 = {  };
    	
    	printf("\nEnter the dollars, quarters, dimes in purse 1 : \n");
    	
    	scanf("%d%d%d", &dQD1.dollars, &dQD1.quarters, &dQD1.dimes);
    	
    	printf("\nEnter the dollars, quarters, dimes in purse 2 : \n");
    	
    	scanf("%d%d%d", &dQD2.dollars, &dQD2.quarters, &dQD2.dimes);
    	
    	sum.dollars = (dQD1.dollars + dQD2.dollars) * 100;  
    	sum.quarters = (dQD1.quarters + dQD2.quarters) * 25;
    	sum.dimes = (dQD1.dimes + dQD2.dimes) * 10;
    	
    	while (sum.quarters > 99)
    		{
    		sum.dollars = sum.dollars + 100;
    		sum.quarters = sum.quarters - 100;	
    		}
    	
    	while (sum.dimes > 99)
    		{
    		sum.dollars = sum.dollars + 100;
    		sum.dimes = sum.dimes - 100;	
    	    }
    		
    	while (sum.dimes > 24)
    		{
    		sum.quarters = sum.quarters + 25;
    		sum.dimes = sum.dimes - 25;		
    		}
    	
    	    sum.dollars = sum.dollars / 100;
    	    sum.quarters = sum.quarters / 25;
    	    sum.dimes = sum.dimes / 10;
    					    
    printf("\n   %d dollars %d quarters %d dimes\n+  %d dollars %d quarters %d dimes\n----------------------------\n   %d dollars %d quarters %d dimes \n", dQD1.dollars, dQD1.quarters, dQD1.dimes, dQD2.dollars, dQD2.quarters, dQD2.dimes, sum.dollars, sum.quarters, sum.dimes);
    			
    return 0;
    }
    why am i getting 6 quarters? there should never be more than 3 quarters.
    Name:  ass8error.PNG
Views: 61
Size:  49.9 KB

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    I agree with grumpy:
    Quote Originally Posted by grumpy
    You would be better off converting the input into dollars and cents, and then convert the number of cents back to quarters and dimes.
    Create a variable named cents. Compute and store the total number of cents in the quarters and dimes into this cents variable. Then, compute the maximum number of dollars from this total number of cents, adding that figure to the total number of dollars and subtracting 100 times of that from the total number of cents. After that, compute the maximum number of quarters from the remaining total number of cents such that you are left with a whole number of dimes.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,661
    Why are you using subtraction instead of division? Even if it's not perfect.

    <num quarters> / <4 quarters> = <num dollars>

    <num dimes> * 10 / <25 cents> = <num quarters> Scaling by a factor of ten eliminates the need to do floating point calculations.

    I'm not sure I get it. I mean, every denomination of dollars is important. Even pennies, which everybody hates.

  10. #10
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    Quote Originally Posted by laserlight View Post
    Create a variable named cents..
    my assignment was to use just the 3 variables i have already used.

  11. #11
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    Quote Originally Posted by whiteflags View Post
    Even pennies, which everybody hates.
    i am trying to change everything to pennies, and then change it back.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    Quote Originally Posted by rosemary
    my assignment was to use just the 3 variables i have already used.
    Does the requirements really say that you are not to use any other variables, or does it say that you must use this struct to store the input and final result?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    it says "use 3 simple variables".
    but, i think i got it.
    i should have been starting with the smallest coin instead.
    Code:
    #include <stdio.h>
    
    
    struct dollarsQuartersDimes
    {
    	int dollars,
    	    quarters,
    	    dimes;
    };
    
    
    int main(void)
    {
    	dollarsQuartersDimes dQD1, sum, dQD2 = {  };
    	
    	printf("\nEnter the dollars, quarters, dimes in purse 1 : \n");
    	
    	scanf("%d%d%d", &dQD1.dollars, &dQD1.quarters, &dQD1.dimes);
    	
    	printf("\nEnter the dollars, quarters, dimes in purse 2 : \n");
    	
    	scanf("%d%d%d", &dQD2.dollars, &dQD2.quarters, &dQD2.dimes);
    	
    	sum.dollars = (dQD1.dollars + dQD2.dollars) * 100;  
    	sum.quarters = (dQD1.quarters + dQD2.quarters) * 25;
    	sum.dimes = (dQD1.dimes + dQD2.dimes) * 10;
    	
        
    	while (sum.dimes > 24)
    		{
    		sum.quarters = sum.quarters + 25;
    		sum.dimes = sum.dimes - 25;		
    		}
    
    
        while (sum.quarters > 99)
    		{
    		sum.dollars = sum.dollars + 100;
    		sum.quarters = sum.quarters - 100;	
    		}
    		
    	sum.dollars = sum.dollars / 100;
    	sum.quarters = sum.quarters / 25;
    	sum.dimes = sum.dimes / 10;
    					    
    printf("\n   %d dollars %d quarters %d dimes\n+  %d dollars %d quarters %d dimes\n----------------------------\n   %d dollars %d quarters %d dimes \n", dQD1.dollars, dQD1.quarters, dQD1.dimes, dQD2.dollars, dQD2.quarters, dQD2.dimes, sum.dollars, sum.quarters, sum.dimes);
    			
    return 0;
    }

  14. #14
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    thanks for the help everybody

  15. #15
    Registered User
    Join Date
    May 2009
    Posts
    2,543
    Hint: If you only have three denominations dollars, quarters, and dimes.
    The total value will end with either 5 or 0; if 5 it means you have an old number of quarters.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21