Thread: algorithm question?

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    60

    algorithm question?

    I'm writing this program that takes in a dollar amount (a) and then displays the the balance (t) after (n) number of years that pays (r) percent interest, compounded annually. My program isn't displaying the results correctly. Can someone tell me if this is at least right?:

    Code:
    t=a*1+pow(r,n);

  2. #2
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    http://en.wikipedia.org/wiki/Compoun...pound_interest says your formula may be wrong.

    Express the problem mathematically, then attempt to convert it to a C expression.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Location
    Sydney
    Posts
    60
    Yes, your formula is wrong. It's *almost* right for simple interest, but definitely not compound interest. Compound interest involves the sum of a geometric series. I'll be happy to explain it if, after doing some research, you still don't know how to do it.

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    60
    I found the formula on the net that explains the way to compute compound interest a=p(1+r\n)nt where a = amount after time t,
    p = principal amount, r = annual interest rate, n = number of time the interest is compounded per year and t = number of years. Do I need to declare another variable? I'm still a little confused because my program below displays the following result:

    "Your balance after 2289404 years with an interest rate of Inf would be $-0.000000."

    ...if I enter a dollar amount deposited of 1500.00 with .07 interest and 20 years for the input.

    Code:
    /* 
       Filename:    balance.c
    
       Description: Displays the balance with interest compounnded annually, 
                    after number of years entered.
    
    */
    
    # include <stdio.h>
    # include <math.h>
    
       void introduction (void);
    
       double ComputeBalance (double a, double r, int n, double t);
    
    
     int main()
    
      {
      
      /*Allocate memory for data*/
    
       introduction ();
       
       double a,r,t;
    
       int n;
    
      /*Prompt user for data*/
    
         printf ("\nEnter the the dollar amount deposited: $");
         scanf ("%lf",&a);
    
         printf ("\nEnter the interest rate:");
         scanf ("%lf",&r);
    
         printf ("\nEnter the number of years:");
         scanf ("%d",&n);
    
      /*Processing and display results*/
    
         printf ("\nYour balance after %d years with an\n");
    
         printf ("interest rate of %lf would be" 
    " $%lf\n",ComputeBalance(n,r,t,a));
    
     return 0;
     
      }
    
         void introduction (void)
    
          {
    
           printf ("\nThis program displays the balance with interest,\n");
           printf ("compounded annually after number of years entered.");
    
          }
    
         double ComputeBalance(double a,double r, int n, double t)
    
          {
    
    
            return t=a*1+pow(r,n);
    
          }

  5. #5
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Well, one problem (not the only one) is that you call it as:
    Code:
    ComputeBalance(n,r,t,a)
    Yet the function has the parameters in this order:
    Code:
    double ComputeBalance(double a,double r, int n, double t)
    So the n in main is passed to the a in ComputeBalance, r to r, t to n, and a to t. Is this what you want to do?

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    60
    OK...I changed

    Code:
    ComputeBalance(n,r,t,a)
    to...

    Code:
    ComputeBalance(a,r,n,t)
    now I get...

    "Your balance after 2289404 years with an interest rate of Inf would be $0.000000."

    ...if I enter a dollar amount deposited of 1500.00 with .07 interest and 20 years for the input.

  7. #7
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Code:
    printf ("\nYour balance after %d years with an\n");
    You supply a %d to the format argument, but you're not passing anything along with it! You _must_ pass a integer with that specifier.

    Code:
    printf ("interest rate of %lf would be" 
    " $%lf\n",ComputeBalance(n,r,t,a));
    That printf() statement needs work as well, and your compiler should not let you compile the program.

    Your ComputeBalance() function is wrong. First, this line:
    Code:
    return t=a*1+pow(r,n);
    Why assign the answer to t? You never use t again. The "t=" has no effect. Also, as others in this thread have said, your formula for compounded interest is wrong. I suggest you read the wiki article that cwr posted.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  8. #8
    Registered User
    Join Date
    Mar 2005
    Posts
    60
    Wouldn't my scanf for interest that is inputted pass to the format arguement?

    Code:
    printf ("\nEnter the number of years:");
         scanf ("%d",&n);
    I changed my format argument to this:

    Code:
        printf ("\nYour balance after %d years with an\n");
    
         printf ("interest rate of %lf would be" 
                   " $%lf\n",n,r,ComputeBalance(a,r));
    Doesn't the scanf prior to my output statement pass the n variable? I removed the t variable from my program but I'm still struggling with the compounded interest. I'm sorry...I'm such a rookie when it comes to C.

  9. #9
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    You're very confused. You need to read up on formatted input/output in your C book.

    As Cactus_Hugger said:
    Code:
    printf ("\nYour balance after %d years with an\n");
    is wrong. That says: output "\nYour balance after %d years with an\n" but replace %d with an integer passed to the function. printf looks for the integer, can't find it, and prints garbage. You need:
    Code:
    printf("\nYour balance after %d years with an\n", n);
    Further, the second line:
    Code:
    printf ("interest rate of %lf would be" 
                   " $%lf\n",n,r,ComputeBalance(a,r));
    Is also wrong. You have two format specifiers (%lf twice) but three parameters. Programming is a very precise thing. You cannot just keep trying stuff until it works, you need to understand what you're doing.

    As for the compound interest formula, it seems to be the least of your problems, but once your code is correct (the printf format specifiers are correct and match up appropriately), tell us what you think the formula for compound interest is, given your variables, and then what C expression you think calculates that formula, and we'll help you if there are any errors.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick Algorithm Question
    By cjwenigma in forum C++ Programming
    Replies: 3
    Last Post: 11-01-2007, 10:39 AM
  2. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  3. STL algorithm question
    By Reggie in forum C++ Programming
    Replies: 1
    Last Post: 04-22-2003, 09:04 AM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  5. More a math question than an algorithm
    By Gustaff in forum C Programming
    Replies: 1
    Last Post: 01-28-2003, 01:10 PM