Thread: creating and using functions

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    2

    creating and using functions

    hey guys I have a hard time finding out how to create a function to run in a program that I'm supposed to do.

    In the middle of this page (highlighted in pink) is a formula for calculating the amount I’d have in the bank after t years if I invested an amount P at an annual interest rate of r, and the interest is compounded n times per year. Write a function called interest which calculates the interest I’ve earned after m months. The function should accept 4 arguments: the principal amount P, the annual interest rate r, the number of times interest is compounded each year n, and the number of months m that the money has been in thebank. The output of the function should be the interest earned. Then write a program which does the following. The user should enter an initial investment P and an annual rate r. The program should then generate a table giving the interest after 3 months, 6 months, 9 months, and 12 months for interest compound annually, monthly,
    and daily (assume 365 days/year). You must use the function interest in your program. The name of this program must be IntTable.c

    Important Note: The interest earned is not equal to A! A is the total amount in the bank, the interest earned is the final amount minus the initial amount.

    A = P * ( 1 + r/n ) ^(nt)

    where,
    P = principal amount (initial investment)
    r = annual interest rate (as a decimal)
    n = number of times the interest is compounded per year
    t = number of years
    A = amount after time t

    Example output for Part A:
    % a.out

    Enter your initial investment P: 2500.00
    Enter the annual interest rate r: 0.04
    Interest earned for different compounding schemes is as follows:
    Investment Time Annually Monthly Daily
    3 months 24.63 25.08 25.12
    6 months 49.51 50.42 50.50
    9 months 74.63 76.01 76.13
    12 months 100.00 101.85 102.02
    %

    so far i only have the headings: Investment Time, Annually, Monthly, Daily,
    and 3 months, 6 months, 9 months, 12 months set correctly.

    I'm having lots of trouble creating the function though,

    Code:
    #include <stdio.h>
    #include <math.h>
    int interest( float y );
    
    /* function main begins program execution */
    int main()
    {
    
    
    
       float P;
       float r;
       int n; 
       float t;
       int A;
    
     printf( "\n enter your initial ivestment P: " );
     scanf( "%.2f", &P );
    
     printf( "enter the annual interest rate r: " );
     scanf( "%.2f", &r );
    
     printf( "\n%15s%10s%9s%8s\n", "Investment Time", "Annually", "Monthly", "Daily" );
    
    printf( "        3 months\n" );
    printf( "        6 months\n" );
    printf( "        9 months\n" );
    printf( "      12 months\n" );
    
    for ( A == 1 ; A <= 4 ; A++ ) {
          printf( "%20d  ", interest ( A ) );
    
    }
    
          return 0;
    
    }
    
    int interest( float y )
    {
           int a;
           int b;
            int c;
            int d;
            int pow;
               
          y = a * pow( 1 + b / c, c * d );
          y -= a;
          return y;
    }
    whenever i try to compile using gcc -lm IntTable.c I keep getting the error:

    IntTable.c:54: error: "called object is not a function"

    Can YOU help me please?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > IntTable.c:54: error: "called object is not a function"
    You've got a variable called 'pow' (for no good reason at all), and you're trying to call a function called pow.

    The next problem is a,b,c,d in your function are
    - have lousy names, what do they mean?
    - uninitialised, you're computing with garbage data.
    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.

  3. #3
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    From dwk on cross post:
    Quote Originally Posted by dwk
    Don't cross-post. http://cboard.cprogramming.com/showthread.php?t=95247
    Click on the submit button only once -- it will get through eventually.
    Code:
    for ( A == 1 ; A <= 4 ; A++ ) {
    No doubt you meant = (for assignment) instead of == (for comparison).
    Code:
    gcc -lm IntTable.c
    On some compilers, you have to put the libraries last. Try
    Code:
    gcc IntTable.c -lm
    I don't know which line is line 54. Next time, try marking it -- you'll probably get a faster response.

    Your printf statements probably won't work as you want them to. Remember that you have to print all of the data you want on a line before you print a newline. Once you print a newline, you can't modify that line at all any more.

    Oh and by the way, your variable names can be longer than one character.
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

Popular pages Recent additions subscribe to a feed