Thread: pow()

  1. #1
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110

    Unhappy pow()

    I am new to C++, I am trying to calculate pow(2,5). What is wring with my codes....can't make it to work !!!!.........

    Code:
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    
    
    int pow(int,int);
    int main ()
    {
    	cout << pow(2,5) << "\n";
    	             
    
    	return 0;
    }

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    where's "pow" defined?

    edit: I meant where is it declared really. you've included a file (math.h indirectly through cmath) that has that declaration in it. you shouldn't need to redeclare it
    Last edited by FillYourBrain; 09-30-2002 at 08:57 AM.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>can't make it to work
    Be more specific, tell us the error messages.

    And look at the prototype for pow(), to see what type of variables it takes as arguments.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110

    Angry

    I am so confused now.....what is missing in my program?


    Code:
    #include <iostream>
    #include <iomanip>
    #include <cmath>
    
    int pow(int i,int n);
    int main ()
    {
    
    	cout << pow(2,5) << "\n";
    
    	return 0;
    }	
    
    int pow(int i,int n)
    {
    
    	return;
    }

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>I am so confused now.....what is missing in my program?
    As I said before: Be more specific, tell us the error messages. And to expand, are you trying to write your own pow() function, or use the supplied one?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >cout << pow(2,5) << "\n";
    You may need to prefix cout with std:: to account for namespaces.

    >return;
    Your pow function is supposed to return an int, yet you return nothing. Place your power calculation here and return the result. Also, if you include a header with a function of the same name, change the name. Unless you are familiar with the intricacies of interpositioning, don't use the same names as the standard libraries or do not include the header if you don't need it.
    Code:
    #include <iostream>
    
    int pow ( int i, int n );
    
    int main()
    {
      std::cout << pow ( 2, 5 ) << "\n";
    
      return 0;
    }	
    
    int pow ( int i, int n )
    {
      int ret = 1;
    
      // Calculate your power here, place the result in ret
    
      return ret;
    }
    My best code is written with the delete key.

  7. #7
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110
    I want to write to function one with prototype

    int pow (int,int) and another with prototype double pow(double,double)

    I want to then print the result 2 to the power of 5 as an int and as a double.


    I get an error message saying cout is undefined
    and << illegal left operand has type, unknown type

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    there is a predefined function called pow() in cmath. If you want to use it I would do this:

    Code:
    #include <iostream>
    #include <cmath>
    
    int main ()
    {
    
    	std::cout << pow(2,5);
    
    	return 0;
    }
    This pow() function accepts two doubles (here 2^5) and returns a double.

    If you want to create your own power function then I would leave out the cmath header and do as Prelude suggests. If you want to include the cmath header and have your own function in the same program so you can test your version out, then I would rename your funciton power() or something other than pow() so as not to confuse your function with the one in cmath.

  9. #9
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110
    thanks guys

    I think I understand the concept now

    But how do you write the math to calculate 2 to the power of 5??

    This is what I have so far:
    Code:
    #include <iostream>
    
    int pow(int i,int n);
    int main ()
    {
    	std::cout << pow(2,5) << "\n";
    
    	return 0;
    }	
    
    int pow(int i,int n)
    {
    	int calc=1;
    
    	calc= ((i*i)*n)-i;
    
    	return calc;
    }

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >But how do you write the math to calculate 2 to the power of 5??
    Using your argument names, loop n times and multiply calc by i. So the body of your pow function would look something like this:
    Code:
    int pow ( int i, int n )
    {
      int ret = 1;
    
      while ( n > 0 ) {
        ret *= i;
        --n;
      }
    
      return ret;
    }
    -Prelude
    My best code is written with the delete key.

  11. #11
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    As an alternative, if all you want to do is print the result as a int and or a double then you could use the prewritten pow() function as cast the result to be an int, rather than a double, instead of writing a new function.

    The error message means you have to explicitly indicate which namespace cout is in, that is namespace std; by either using the std::cout syntax or by using a line like this, usually as a global declaration:

    using namespace std;

    The prefered way is the std::cout syntax, but it gets pretty noxious writing all the std:: thingys so many people use the other syntax instead. I think there is a third option, too; use a line like:

    using std::cout;

    as a global statement instead of including the entire std namespace in global scope.

  12. #12
    Registered User Max's Avatar
    Join Date
    Jul 2002
    Posts
    110
    Of course it did not occur to me that I can use a while look

    Thanks.....

    Yes I am aware that I can use the cmath but I don't want to because then it is too easy.

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Yes I am aware that I can use the cmath but I don't want to because then it is too easy.
    Hehe, I like you, you remind me of myself. I always prefer to do things the hard way for fun simply to say I could. Then I learn how to do it the easy way for any real coding.

    -Prelude
    My best code is written with the delete key.

  14. #14
    Hehe... my knack at doing that kind of thing annoyed my comp science (a.k.a. programming) teacher to no end.....
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

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, 02:17 AM
  3. trying to use pow()
    By Mikecore in forum C Programming
    Replies: 9
    Last Post: 10-09-2005, 06:30 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, 11:54 AM