Thread: a dime off.

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Try entering 0 0 5 for the first purse and 0 0 4 for the second purse. You will find that the result is 3 quarters and 1 dime, but it should be 2 quarters and 4 dimes, i.e., you still have a bug related to computing of the number of quarters such that the number of dimes is a whole number.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I suggest testing the program with

    0 0 1
    0 0 2

    I think the correct answer is 0 0 3 but, I think your code is likely to get 0 1 0 as the answer.

    Another good input to test is this

    0 1 3
    0 1 2

    I think the correct answer is 1 0 0

    Tim S.
    Last edited by stahta01; 10-22-2012 at 09:43 PM. Reason: removed + sign
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #18
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    bummer!

  4. #19
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    ok. i added the cents variable. thanks laserlight! i know i still don't solve the problem laserlight mentioned about 3 dimes turning into 1 quarter instead of staying 3 dimes (and not losing that 5 cents). but, my brain hurts. so i'm going to sleep on this code.
    Code:
    #include <stdio.h>
    
    
    struct dollarsQuartersDimes
    {
    	int dollars,
    	    quarters,
    	    dimes;
    };
    
    
    int main(void)
    {
    	int cents;
    	
    	dollarsQuartersDimes dQD1, dQD2, dQD3 = {  };
    	
    	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);
    	
    	dQD3.dollars = (dQD1.dollars + dQD2.dollars) * 100;  
    	dQD3.quarters = (dQD1.quarters + dQD2.quarters) * 25;
    	dQD3.dimes = (dQD1.dimes + dQD2.dimes) * 10;
    	cents = dQD3.dollars + dQD3.quarters + dQD3.dimes;
    	dQD3.dollars = 0;
    	dQD3.quarters = 0;
    	dQD3.dimes = 0;
        
    	while (cents > 99)
    		{
    		++dQD3.dollars;
    		cents = cents - 100;		
    		}
    
    
        while (cents > 24)
    		{
    		++dQD3.quarters;
    		cents = cents - 25;	
    		}
    		
    	while (cents > 9)
    		{
    		++dQD3.dimes;
    		cents = cents - 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, dQD3.dollars, dQD3.quarters, dQD3.dimes);
    			
    return 0;
    }

  5. #20
    Registered User
    Join Date
    Sep 2012
    Posts
    99
    Code:
    while (cents > 30)
    		{
    		++dQD3.quarters;
    		cents = cents - 25;

Popular pages Recent additions subscribe to a feed