late night coding

This is a discussion on late night coding within the C Programming forums, part of the General Programming Boards category; Allright so I know the error in my code is a logic error. I"m not very good at math, and ...

  1. #1
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751

    late night coding

    Allright so I know the error in my code is a logic error. I"m not very good at math, and even if i were i'm not sure i would be able to translate it to C.

    The problem. Daphne earns 10% simple interest every year and Deirdre earns 5% compound interest. After how many years will Deirdre's balance exceed Daphne.?

    I coded it the first time and every thing seemed fine. Then I realised deirdre had to have compound interest. I went through it a couple times, but it might as well be obfuscated code for me at this hour. What if anything Am i doing wrong, becaue my answer can't be right, is it?
    Code:
    /* the CompoundINTREST.c */
    
    
    #include <stdio.h>
    #define AMNT 100.00
    
    int main(void)
    
    {
    
        float daphneBAL, deirdreBAL, interest;
        int year;
    
    
        /* Format the screen */
    
        printf("Year     Daphne\t  Deirdre\n");
    
        /*calculate interest */
    
        deirdreBAL = 100.00;
        for ( year = 1; year <= 10; year++){
            daphneBAL = ( AMNT * 0.10 + AMNT) * year;  /* ten percent simple interest */
    
            /* deirdre gets compounded interest */
    
            interest = ( AMNT * 0.05 + AMNT) * year;
            deirdreBAL += interest;
    
            printf("%3d  %10.2f  %10.2f", year, daphneBAL, deirdreBAL);
            if ( deirdreBAL > daphneBAL ){  /* is deirde beating daphne yet */
               printf("\nDeirdre just beat daphne\n");
               break;
               }
         printf("\n");
         }
    
    
    
    
         getchar();
         return(0);
    
    
     }

    Thank you (C board);

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,854
    Your maths is screwed basically,

    Code:
        /*calculate interest */
        daphneBAL  = AMNT;
        deirdreBAL = AMNT;
        for (year = 1; year <= 50; year++) {
            daphneBAL  += AMNT * 0.10;    /* ten percent simple interest */
            deirdreBAL *= 1.05;           /* 5 percent compounded */
            printf("%3d  %10.2f  %10.2f", year, daphneBAL, deirdreBAL);
            if (deirdreBAL > daphneBAL) {   /* is deirde beating daphne yet */
                printf("\nDeirdre just beat daphne\n");
                break;
            }
            printf("\n");
        }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Thanks salem,

    I'll have to work on that.

    Do you have any good links or tutorials on mathamatical thinking as it applies to programming and computers. I've been searching google but to no avail. Or is discreete mathamatics the way to go - aside from arithmatic, and algebra and linear?

    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DirectShow code - long night of coding
    By VirtualAce in forum Game Programming
    Replies: 2
    Last Post: 03-27-2006, 10:50 AM
  2. Late night part 2
    By caroundw5h in forum C Programming
    Replies: 5
    Last Post: 09-09-2004, 11:07 AM
  3. late night coding
    By caroundw5h in forum C Programming
    Replies: 18
    Last Post: 09-05-2004, 08:37 AM
  4. Good times, late at night
    By prog-bman in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 06-15-2004, 10:02 AM
  5. friday night coding with mind numbing music
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 07-13-2002, 06:17 PM

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