trying to use pow()

This is a discussion on trying to use pow() within the C Programming forums, part of the General Programming Boards category; im trying to use the power function pow() but when I do the program fails to compile with undefind refer ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    23

    trying to use pow()

    im trying to use the power function pow() but when I do the program fails to compile
    with undefind refer to pow


    Code:
    // calculate loan payments on a mortgage
    
    
    #include <stdio.h>
    #include <math.h>
    
    int main()
    
    {
    
            // declare all variables
    
            double principal_amount = 0;
            double interest_rate = 0;
            int years = 0;
    
    
            system ("clear");
    
            printf ( " This program will calculate your monthly payment of a Home Mortgage\n\n");                                               
            printf ( " First it will need some info \n\n" );
            scanf  ( " Please enter the loan amount:\t %d \n\n", &principal_amount );
            scanf  ( " Next how many years would you like the loan for?\t %d \n\n", &years );
            scanf  ( " And last what intrest rate are you getting?\t %d \n\n", &interest_rate );
    
    
            int number_of_payments = years * 12;
            double monthly_interest = interest_rate / 12;
            double  x = pow( 1.0 + monthly_interest, number_of_payments );
            double monthly_payment = ( principal_amount * x * monthly_interest ) / ( x - 1 );
    
    
            printf ( "\n\n Your monthly payment would be %d.2\n\n ", monthly_payment );
            
    
            return 0;
    
    }

  2. #2
    Frustrated C Programmer
    Join Date
    Sep 2005
    Location
    Florida
    Posts
    5
    Your problem is that you are declaring and initializing variables later in the program. A good, general guide to follow is to declare and initialize all of your variables (if they need to be initialized -- and initialize them with numbers) at the start of your program and then calculate the values for them later.

    I did a test run with your program, declaring all your variables at the start and then calculating their values later, and it compiles correctly. Whether it works correctly or not from there, is up to you to test!

    P.S. You used pow() correctly.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,643
    The way you've done things (declaring variables later) is C++, not C. Move declarations to the top of the function to address that.

    Apart from that, your problem isn't compilation. It is more likely to be a linker problem. With a lot of development environments, the math library is not linked in by default.

    With Unix compilers (eg gcc) this is addressed by adding "-lm" to the command line. With windows development environments, there is usually an option somewhere concerned with using floating point libraries.

    An alternative, in your case, is to replace;
    Code:
    double  x = pow( 1.0 + monthly_interest, number_of_payments );
    with;
    Code:
    double temp = 1.0 + monthly_interest;
    double x = 1.0;
    for (int i = 0; i < number_of_payments; ++i)
        x *= temp;
    Last edited by grumpy; 10-09-2005 at 12:57 AM.

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    23

    yup it was a linker problem

    yea it was a linker problem I figured that out

    I also found many other problems with my code so I fixed it

    Code:
    // calculate loan payments on a mortgage
    
    
    #include <stdio.h>
    #include <math.h>
    
    int main()
    
    {
    
            // declare all variables
    
            double principal_amount = 0;
            float interest_rate = 0.0 ;
            int years = 0;
    
    
            system ("clear");
    
            printf ( " This program will calculate your monthly payment of a Home Mortgage\n\n" );
            printf ( " First it will need some info \n\n" );
            printf ( " Please enter the loan amount: " );
            scanf  ( "%d", &principal_amount );
            printf ( "\n\n Next how many years would you like the loan for? " );
            scanf  ( "%d", &years );
            printf ( "\n\nAnd last what intrest rate are you getting? " );
            scanf  ( "%3f", &interest_rate );
    
    
            int number_of_payments = years * 12;
            float monthly_interest = interest_rate / 12;
            double monthly_payment = principal_amount * pow ( 1.0 + monthly_interest, number_of_payments )  * monthly_interest / ( pow ( 1.0 + monthly_interest, number_of_payments ) - $
    
    
            printf ( "\n\n Your monthly payment would be $%d dollars\n\n ", monthly_payment );
            printf ( "\n Principal amount is %d", principal_amount );
            printf ( "\n Number of payments is %d", number_of_payments );
            printf ( "\n Monthly interest is %f", monthly_interest );
            printf ( "\n Interest rate is %3f\n", interest_rate );
    
            return 0;
    
    }

    it runs now

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,830
    In addition, some printf and scanf calls are wrong
    foo.c:21: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘double *’
    foo.c:30: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’
    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.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,643
    For float and double, you need to use %f or %g format specifiers rather than %d (which is for int).

    You're lucky; not all compilers check that for you.

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    23
    Quote Originally Posted by grumpy
    For float and double, you need to use %f or %g format specifiers rather than %d (which is for int).

    You're lucky; not all compilers check that for you.
    I looked at what your are saying and I did some reading and some testing.

    my school book say that you should express an dollar amount as a "%d" format specifer
    cuase your talking about money. But I did see the warnings the other person got.

    So how could I correct this. I tried changing both the "%f" but after I recompiled it my
    awnser was no-longer correct.

    I tested the program with 100,000 dollar @ 7% interest rate over 30 years. it should
    come back with a $665 monthly payment. after making the format change my answer was
    wrong.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Your example does not compile because of this line (split for readability):
    Code:
    double monthly_payment = principal_amount * \
        pow ( 1.0 + monthly_interest, number_of_payments )  * \
        monthly_interest / ( pow ( 1.0 + monthly_interest, number_of_payments ) - $
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
    double principal_amount = 0;
    scanf  ( "%d", &principal_amount );
    %f is for floats, %lf is for doubles, and %d is for integers.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by dwks
    Your example does not compile because of this line (split for readability):
    Code:
    double monthly_payment = principal_amount * \
        pow ( 1.0 + monthly_interest, number_of_payments )  * \
        monthly_interest / ( pow ( 1.0 + monthly_interest, number_of_payments ) - $
    The \'s at the end of the first two lines in the above code aren't necessary.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's the difference between pow() vs shift?
    By Overworked_PhD in forum C Programming
    Replies: 9
    Last Post: 01-06-2008, 01:04 PM
  2. Help with pow()
    By jedi_jinn in forum C Programming
    Replies: 7
    Last Post: 10-09-2006, 03:17 AM
  3. alternatives to pow()
    By mcdms in forum C++ Programming
    Replies: 4
    Last Post: 10-10-2004, 10:42 PM
  4. pow function returns wrong value
    By ckbales in forum C Programming
    Replies: 5
    Last Post: 02-01-2003, 09:46 PM
  5. Problem with pow()
    By RMacFly in forum C Programming
    Replies: 4
    Last Post: 09-18-2001, 12:54 PM

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