Thread: creating a "to the power" equivalent in c

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    11

    creating a "to the power" equivalent in c

    hi there, im using a while command to generate a number sequence and in the formula i am using, i need to raise -1 to the power of (k+1)

    the whole equation is 2*k*(-1)to the power of (k+1) any help would be appreciated

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What's wrong with pow? (Although (-1) to any power is either -1 or +1 so pow may be overkill.)

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    11
    it is to generate 2, -4, 6, -8 but with just pow, it says syntax error, missing ")" before identifier "pow"

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Post the smallest and simplest program that demonstrates the error.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Show your code.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    11
    Code:
        k = 0;
        while ( k < 20 )
        {
            printf("\n\t\t The numbers are %4d", 2*k*(-1)pow(k+1));
            k = k + 1;
        }

  7. #7
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    pow requires two arguments...pow(base, exponent)...also, pow returns a double or some other kind of floating point number, not a decimal...instead of thinking in terms of pow, use the % operator...look at the (-1)^(k+1) sequence:

    k | f(k)
    0 | -1
    1 | 1
    2 | -1
    3 | 1

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    11
    Quote Originally Posted by Epy View Post
    pow requires two arguments...pow(base, exponent)...also, pow returns a double or some other kind of floating point number, not a decimal...instead of thinking in terms of pow, use the % operator...look at the (-1)^(k+1) sequence:

    k | f(k)
    0 | -1
    1 | 1
    2 | -1
    3 | 1
    so would i do 2*k*pow(-1, k+1)

    ?

    edit i tried this and generated 0, 2488, 4976, and so on with huge numbers
    Last edited by Irony; 10-06-2009 at 09:18 AM.

  9. #9
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    I don't think pow() accepts negative bases. Like I said, look at the modulo (%) operator...when k is even, 2k(-1)^(k+1) is negative...use that to your advantage.

  10. #10
    Registered User
    Join Date
    Sep 2009
    Posts
    11
    that kind of just lost me.

    we just learned while commands and he wants us to generate 2, -4, 6, -8, and so on for 20 terms. i have no idea what a modulo or % operator is. we havnt learned that yet.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Are you required to use pow()? It would be simpler if you did not use it, but instead relied on say, the fact that -x = -1 if x = 1, but -x = 1 if x = -1, and thus use the expression -x * n. (You can actually do without multiplication.)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Sep 2009
    Posts
    11
    no im not required to use pow, but i figured it woudl be easiest in 2*k*(-1)^k that generates 2, -4, 6, -8, and so on...

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Ah, then you should recognise that (-1)^k is just mathematical notation to express alternating coefficients of 1 and -1.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Sep 2009
    Posts
    11
    so how would i set up that x and stuff in my code?

  15. #15
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Quote Originally Posted by tabstop View Post
    What's wrong with pow? (Although (-1) to any power is either -1 or +1 so pow may be overkill.)
    -1 to the power of 2.5 is neither 1 nor -1.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. Pointer equivalent to array notation
    By bekkilyn in forum C Programming
    Replies: 4
    Last Post: 12-06-2006, 08:22 PM
  3. Replies: 10
    Last Post: 08-17-2005, 11:17 PM
  4. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 11:31 PM
  5. Help creating lists of pointers
    By The_Kingpin in forum C Programming
    Replies: 2
    Last Post: 12-11-2004, 08:10 PM