# creating a "to the power" equivalent in c

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 10-06-2009
Irony
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
• 10-06-2009
tabstop
What's wrong with pow? (Although (-1) to any power is either -1 or +1 so pow may be overkill.)
• 10-06-2009
Irony
it is to generate 2, -4, 6, -8 but with just pow, it says syntax error, missing ")" before identifier "pow"
• 10-06-2009
laserlight
Post the smallest and simplest program that demonstrates the error.
• 10-06-2009
hk_mp5kpdw
• 10-06-2009
Irony
Code:

```    k = 0;     while ( k < 20 )     {         printf("\n\t\t The numbers are %4d", 2*k*(-1)pow(k+1));         k = k + 1;     }```
• 10-06-2009
Epy
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
• 10-06-2009
Irony
Quote:

Originally Posted by Epy
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
• 10-06-2009
Epy
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-06-2009
Irony
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.
• 10-06-2009
laserlight
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.)
• 10-06-2009
Irony
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...
• 10-06-2009
laserlight
Ah, then you should recognise that (-1)^k is just mathematical notation to express alternating coefficients of 1 and -1.
• 10-06-2009
Irony
so how would i set up that x and stuff in my code?
• 10-06-2009
abachler
Quote:

Originally Posted by tabstop
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.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last