Newbie Help. I can't use pow and call the math library.

This is a discussion on Newbie Help. I can't use pow and call the math library. within the C Programming forums, part of the General Programming Boards category; I am writing a calculator program. I am new to C. I am having problem with the expontential function. This ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    93

    Smile Newbie Help. I can't use pow and call the math library.

    I am writing a calculator program. I am new to C. I am having problem with the expontential function. This program adds, subtracts, etc. I have left off some of the code but I think you get the idea.
    Code:
     # include <stdio.h>
      7 
      8 double power(double int1, int int2);
      9 
     10 int main ( void )
     11 
     12 {
     13 float int1, int2, result, menu;
     14 
    
     else if (menu == 5)
     55 {
     56 
     57  result = power(double int1, int int2);
     58 printf("%.2lf raised to the power %d = %.2lf\n", int1, int 2, power(int1,int    2));
     59 
     double power(double int1, int int2){
     79 if ( int2 == 0)
     80 return int1;
     81 else
     82 return int1 * power (int1, int2 - 1);
     83 }
    line 57 is giving me

    error: expected expression before ‚double‚

    line 57
    error: expected expression before ‚double‚

    line 58
    expected expression before ‚int‚

    line 58

    too few arguments for format

    Thanks for the help in advance.

  2. #2
    Registered User
    Join Date
    Feb 2008
    Posts
    93
    Should more of the code be posted? No responses.

    Thanks

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    87
    Code:
    8 double power(double int1, int int2);
    This is good, you are declaring your function before you use it in main(). An observation:
    - The first parameter is being labeled "int1", although its type is double. A double can represent fractional numbers as well as whole numbers. Are you using that type to allow for integers larger than the int type can support? The name "int1" is irrelevant, but looks misleading.

    Code:
    57  result = power(double int1, int int2);
    What are you trying to do here? power() takes two arguments, like you specified in the declaration above. The first is of type double and the second is of type int. So, if I have these three variables:
    x which is a double,
    y which is an integer, and
    z whcih is a double,
    I can do:
    Code:
    z = power(x, y);
    You do not write "double" and "int" in there before the x and y. That tells you what type of variable to supply.


    Code:
    59 double power(double int1, int int2){
    79 if ( int2 == 0)
    80 return int1;
    81 else
    82 return int1 * power (int1, int2 - 1);
    83 }
    Line 80 probably isn't what you want. Say int1 = 10, int2 = 0...is 10^0 = 10?

    I also thought it was worth pointing out that the standard library already defines a function called pow(). It is defined in math.h.

    Hope that help you out

  4. #4
    ---
    Join Date
    May 2004
    Posts
    1,379
    Don't you need to link to the math library?
    And maybe #include <math.h>

    Don't you think it's kind of confusing to name a float int1 and int2?

  5. #5
    HelpingYouHelpUsHelpUsAll
    Join Date
    Dec 2007
    Location
    In your nightmares
    Posts
    223
    Code:
    double power(double int1, int int2){
     79 if ( int2 == 0)
     80 return int1;
     81 else
     82 return int1 * power (int1, int2 - 1);
     83 }
    Doesn't anything to the power of 0 equal to 1. x to the power of 1 equals x. Should be, note that either one can be 0 due to multiplication:
    Code:
    if ( int2 == 0 || int1 ==0) return 1;
    The power function is undeclared, see last post. To be less confusing call int1 something like double1, int1 sort of implies that it is an int, when it is really a double. I am amazed that someone has shown the initiative to post line numbers. Well Done.
    long time no C; //seige
    You miss 100% of the people you don't C;
    Code:
    if (language != LANG_C && language != LANG_CPP)
        drown(language);

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    53
    Quote Originally Posted by sand_man View Post
    Don't you need to link to the math library?
    And maybe #include <math.h>
    In the thread title, he says he can't use pow() and can't link to the math library. Probably because it's a homework assignment and he is supposed to implement pow() himself.

    Note, BSmith4740, that because you define your power() funtion to be recursive (which your teacher will probably love) yet return 0 if the exponent is 0, your power() will return zero for every input: Suppose you want to calculate 2^3, then you would get
    Code:
    power(2, 3) = 2*power(2, 2) = 2*2*power(2,1) = 2*2*2*power(2,0) = 2*2*2*0 = 0
    Your variable names are indeed chosen rather poorly. I wouldn't call them "double1" and "double2" either. More something like "base" and "exponent" perhaps?

    Good luck,
    Sander

    --
    Computer Programming: An Introdution for the Scientifically Inclined

  7. #7
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    You only need to specify a type (i.e. double) for a function, when you're declaring (like on line 8) or defining (like on line 78) it. This is to tell the compiler that when you call the function (like on line 57), it should only allow variables of that type to be passed.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  8. #8
    Registered User
    Join Date
    Feb 2008
    Posts
    93

    Hard time with functions

    Could anyone be kind enough to do an example of an expontential function using if else statements, while statement, or for statement. I am having a hard time picturing this in my head. I got the function call figured out but I have to take the value and raise it to an expontial function a second time.

    Code:
     double power(double , int int2);  (function prototype)
    
    while(menu=2){
    result = power(  int1,  int2);
    }  (function call)
    
     double power( double int1, int int2) {
    124 if ( int2 == 0)
    125 return 1;
    126 else
    127 return int1 * power (int1, int2 - 1);
    (What I do?)
    I have have tried:
    temp = int3 * power( int1, int2);

    This gives 2 raised to the power of 2 = 4
    It should read 4 raised to the power of 2 = 8
    Any ideas?

    Thanks

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by BSmith4740 View Post
    Could anyone be kind enough to do an example of an expontential function using if else statements, while statement, or for statement. I am having a hard time picturing this in my head. I got the function call figured out but I have to take the value and raise it to an expontial function a second time.
    What does that mean? Do you mean you need to do it not recursively, or you are having trouble with your recursive function, or something else?

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    4 to the power of 2 is 16.
    2 to the power of 2 is 4.
    2 to the power of 3 is 8.

    I don't see anything directly wrong with your code. You may want to guard against negative numbers in int2, since that would be troublesome (it will possibly crash your application). You should also guard against excessively large int2.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    HelpingYouHelpUsHelpUsAll
    Join Date
    Dec 2007
    Location
    In your nightmares
    Posts
    223
    Quote Originally Posted by BSmith4740 View Post
    [code]if ( int2 == 0)
    125 return 1;
    126 else
    127 return int1 * power (int1, int2 - 1);[CODE]
    What if the user tries int2 as -ve e.g. power(0, -3), the function will recurse forever (depending on compiler). It may seem abit pedantic but when making a recursive function you want it to recurse a finite number of times giving atleast an approximate answer. Also note that 0^x = 0 (where x is positvie). Note: you don't want the function being passed int1 = 0 and int2 < 0. You will get that value that the function will never evaluate the exact value of (1/0).
    Last edited by P4R4N01D; 06-10-2008 at 07:23 PM. Reason: Forgot to close tag

Popular pages Recent additions subscribe to a feed

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