Thread: Problems with code

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    132

    Problems with code

    Hi, everyone. I am trying to solve a problem in C: given two accounts with different kinds of interests, both starting with $100,00, how many years does it take for one to have more money than the other? The code is

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    
        int year = 0;
        float ammountDaphne = 100, ammountDeirdre = 99;
    
        while (ammountDaphne > ammountDeirdre)
        {
    
            year++;
    
            if (year == 1)
                ammountDeirdre = 100;
    
            ammountDaphne = ammountDaphne + 0.1*ammountDaphne;
            ammountDeirdre = ammountDeirdre + 0.05*ammountDeirdre;
    
        }
    
        printf("It took %d years for Deirdre's investment to exceed Daphne's one. Now, Deirdre has $ %.2f and Daphne has $ %.2f.\n", year, ammountDeirdre, ammountDaphne);
    
        return 0;
    
    }
    However, when I run the program, the final investiments of both accounts appear with strange symbols; besides, if I change the ammounts from float type to double type, the ammount of years found is different than before. Why are these two things hapenning?

    Thanks in advance
    Last edited by stdq; 08-22-2011 at 07:57 PM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ummmm .... it might help if you could show us the strange symbols... a screen shot if possible...

    As for the different results... when you changed to doubles did you change your printf() at the end to %lf as well?

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    132
    Hi! I tried using %lf for double and float, and had strange symbols in the output in either case. I am sending two console executions of the code, the first for float, and the second for double:
    Attached Images Attached Images Problems with code-image-jpg 

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, that looks the right answer -- given that Daphne gets 10% and Deirdre gets 5% interest, then Deirdre won't have more money until infinity, when they both have infinity dollars.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    You are likely over flowing the double and the float values.

    Why do YOU think that the amount increasing by 10% will ever be less that the amount increasing by 5%?

    Tim S.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Okey dokey...

    First... please don't back edit your messages. That deprives others of the chance to learn from this experience.

    Now... I added a diagnostic print to you code...
    Code:
    #include <stdio.h>
    #include <windows.h>
    
    
    int main(void)
    {
    
        int year = 0;
        float ammountDaphne = 100, ammountDeirdre = 99;
    
        while (ammountDaphne > ammountDeirdre)
        {
    
            year++;
    
            if (year == 1)
                ammountDeirdre = 100;
    
            ammountDaphne = ammountDaphne + 0.1*ammountDaphne;
            ammountDeirdre = ammountDeirdre + 0.05*ammountDeirdre;
    
    printf("%d   %.2f   %.2f\n",year,ammountDaphne,ammountDeirdre);
    Sleep(100);
    
    
        }
    
        printf("It took %d years for Deirdre's investment to exceed Daphne's one. Now, Deirdre has $ %.2f and Daphne has $ %.2f.\n", year, ammountDeirdre, ammountDaphne);
    
        return 0;
    
    }
    The special symbols you are seeing are #INF meaning infinite... indicating a floating point overflow because Deidre never does catch up to Daphne...

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    A float and a double with the same value, may print out slightly different amounts, because they are both (frequently), close approximations, rather than exact amounts.

    That means that the float will round off before the double will, and will have a different value when you print it. Don't compare a float with a double, unless you absolutely have to.

    If you change the data type to double (and you should), then use ONLY %lf with your printout for the amount.

    If you have two accounts, with equal money in them, but they earn different interest, the amount of interest will be different, as soon as the interest that is compounded, is added to the account. What you have to do, I believe, is find the first time that the difference between the accounts is greater than one cent.

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    132
    Actually, it was supposed to be

    Code:
    ammountDaphne = ammountDaphne + 10;
    Now, when I run the program, it works fine, in either float and double versions. I appreciate the help, and apologize for my mistake.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with Code! Having problems.
    By Invertalon in forum C Programming
    Replies: 4
    Last Post: 03-05-2008, 06:17 AM
  2. Code problems.....once again
    By NicAuf in forum C++ Programming
    Replies: 1
    Last Post: 10-31-2007, 02:55 PM
  3. Problems with code
    By skooter211 in forum C++ Programming
    Replies: 11
    Last Post: 06-23-2005, 06:30 PM
  4. Problems with code from the FAQ
    By Bhaunted in forum C++ Programming
    Replies: 1
    Last Post: 08-31-2004, 02:36 PM
  5. Problems with code
    By dasher in forum C++ Programming
    Replies: 6
    Last Post: 01-23-2002, 12:00 PM