# C++ help?

• September 17th 2008, 09:10 PM
amor_vincit_omnia
C++ help?
I need to write a program that sums, term by term, the following poylnomial.

p(x) = 1 + x + x^2 + x^3 + x^4 + ... + x^38

when x = 0.5

Here's the code I did, but the answer is completely wrong, lol:

#include <iostream>
using namespace std;
main()
{
int i, n;
float x;
x=0.5;
n=1;
for (i=0; i<38; i++)
{
n=n*i;
n=1+n;
}
cout<<"The sum term by term is: "<<n;
cin.get();
}

---

Also, it says write the code for the nested multiplication form of that...any idea there?

Thanks.
• September 17th 2008, 10:16 PM
o_O
I don't know any C++ but I know a bit of java and I assume it's pretty similar.

What's going on with that for-next loop of yours. We want the sum of $1 + x + x^2 + \hdots + x^38$ where x = 0.5 but you don't even use it in your for-next loop!

So, if you want to 1 by 1 sum up each term, notice that: $p(0.5) = 0.5^0 + 0.5^1 + 0.5^2 + 0.5^3 + \hdots + 0.5^{38}$.

So the for next loop should be pretty easy to construct:

Code:

```x = 0.5; s = 0; \\Let s be a variable that denotes the sum for (i = 0; i = 38; i++) {   s = s + x^i;      \\Basically, take the sum and add the next power of 0.5 } System.out.Println("p(0.5) = " + s);       \\I forgot if this is how you output it in java```
• September 17th 2008, 11:13 PM
amor_vincit_omnia
LOL @ what's goin' on with my for loop. No idea, I guess I shoulda taken C or C++ before Numerical methods.

Okay, here's the new one, but now the error is on the line: s=s+x^i
The error says: invalid operands of types `float' and `int' to binary `operator^'

--

#include <iostream>
using namespace std;
main()
{
int i,s;
float x;
x=0.5;
s=0;
for (i=0; i=38; i++)
{
s=s + x^i;
}
cout<<"The sum of p(x) term by term is: "<<s;
cin.get();
}
• September 17th 2008, 11:31 PM
flyingsquirrel
Hi,
Quote:

Originally Posted by amor_vincit_omnia
Okay, here's the new one, but now the error is on the line: s=s+x^i
The error says: invalid operands of types `float' and `int' to binary `operator^'

x^y computes x XOR y which is not what you want. To compute $x^y$ you have to use pow(x,y) which comes from the cmath library. (if you're not allowed to do this you'll have to write a function which, given $x$ and $y$, computes $x^y$...)

Quote:

Code:

```#include <iostream> using namespace std; main() {   int i,s;   (...) }```

Are you sure that $s=0.5^0+0.5^1+\cdots$ is an integer?
• September 18th 2008, 12:08 AM
amor_vincit_omnia
Thanks for the very helpful reply. And no, s is not an integer. It compiles, but now returns a blank black screen

#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;
main()
{
int i;
float x,s;
x=0.5;
s=0;
for (i=0; i=38; i++)
{
s= s + pow (x,i);
}
cout<<"The sum of p(x) term by term is: "<<s;
cin.get();
}
• September 18th 2008, 12:35 AM
flyingsquirrel
Quote:

Code:

`#include <stdio.h>`

stdio.h is the C library which corresponds to iostream, there is no need to include it.
Quote:

Code:

`#include <math.h>`

If you're programming in C++ you should include cmath instead of math.h.
Quote:

Code:

```for (i=0; i=38; i++) {     s= s + pow (x,i); }```

This loop will never end because of "i=38". There is a difference between "=" and "==".
Code:

`i=38;`
In this case i takes the value 38, which is not what you want.
Code:

`i==38;`
In this case one tests if i equals 38. That's closer to what you want but your program will still not behave as expected. Try to understand why. (Nod)
• September 18th 2008, 03:47 AM
CaptainBlack
Quote:

Originally Posted by o_O
I don't know any C++ but I know a bit of java and I assume it's pretty similar.

What's going on with that for-next loop of yours. We want the sum of $1 + x + x^2 + \hdots + x^38$ where x = 0.5 but you don't even use it in your for-next loop!

So, if you want to 1 by 1 sum up each term, notice that: $p(0.5) = 0.5^0 + 0.5^1 + 0.5^2 + 0.5^3 + \hdots + 0.5^{38}$.

So the for next loop should be pretty easy to construct:

Code:

```x = 0.5; s = 0; \\Let s be a variable that denotes the sum   for (i = 0; i = 38; i++) {   s = s + x^i;      \\Basically, take the sum and add the next power of 0.5 }   System.out.Println("p(0.5) = " + s);       \\I forgot if this is how you output it in java```

Even if C/C++ used an exponentiation operator rather than the pow function this is not the way to do this. In pseudo-code a better way (still not the most efficient but it is easier to understand than the more efficient and/ot numerical better behaved methods) is:

Code:

```function Series(x)     PowerX=1   SS=0     for idx=0 to 38     SS=SS+PowerX     PowerX=PowerX*x   endfor     return SS   endfunction```
RonL
• September 18th 2008, 04:07 AM
janvdl
Quote:

Originally Posted by amor_vincit_omnia
I need to write a program that sums, term by term, the following poylnomial.

p(x) = 1 + x + x^2 + x^3 + x^4 + ... + x^38

when x = 0.5

Here's the code I did, but the answer is completely wrong, lol:

#include <iostream>
using namespace std;
main()
{
int i, n;
float x;
x=0.5;
n=1;
for (i=0; i<38; i++)
{
n=n*i;
n=1+n;
}
cout<<"The sum term by term is: "<<n;
cin.get();
}

---

Also, it says write the code for the nested multiplication form of that...any idea there?

Thanks.

This is C#.
Code:

``` Main () {   double dNum = 0.5, dTotal = 0;     for(int i; i <= 38; i++)   {       dTotal += Math.Pow(dNum, i);   }     MessageBox.Show(dTotal.ToString()); }```
• September 18th 2008, 08:44 AM
amor_vincit_omnia
Quote:

Originally Posted by flyingsquirrel
stdio.h is the C library which corresponds to iostream, there is no need to include it.

If you're programming in C++ you should include cmath instead of math.h.

This loop will never end because of "i=38". There is a difference between "=" and "==".
Code:

`i=38;`
In this case i takes the value 38, which is not what you want.
Code:

`i==38;`
In this case one tests if i equals 38. That's closer to what you want but your program will still not behave as expected. Try to understand why. (Nod)

I dunno what ==38 is, I was using <=38. It is giving an answer of 2 =/

#include <cstdlib>
#include <iostream>
#include <cmath>
using namespace std;
main ()
{
int i;
float x,s;
x=0.5;
s=0;
for (i=0; i<=38;i++)
{
s=s+pow(x,i);
}
cout <<"The sum of p(x), term by term, is: "<<s;
cin.get();
}
• September 18th 2008, 09:05 PM
Aryth
They're just saying that = and == have different meanings in C++. As you know...

= assigns a value on the right to the variable on the left.

== tells the compiler these two are equal or to compare the equality.

If the compiler sees =, it will never compare, so it will constantly reassign the same value over and over again, resulting in an infinite loop.

But if the compiler sees ==, it will compare the two values and execute the program.

Let's just take a course through the execution of your program:

declare integer i
declare floats x and s

set x to 0.5
set s to 0

for an integer 0 as long as i is less than or equal to 38 then

s = s + pow(x,i);

and increment i by 1 and return.

i=0, so the first incrementation can happen:

s = 0 + pow(0.5,0)

= 0 + 1

i++ = 1

second incrementation can happen:

s = 1 + pow(0.5,1)

s = 1 + 0.5

s = 1.5

It's obvious it will go to 2, but the compiler hit numbers that are outside the float range so it shot to 2.

What you should do is change this line:

float x,s;

to

double x,s;

and if that still gives you 2, then try

long double x,s;

double and long double increase the digit range of your number, allowing you to get better approximations
• September 18th 2008, 10:02 PM
amor_vincit_omnia
I tried both double & long double, and the value is still 2.
• September 18th 2008, 11:13 PM
Aryth
Your problem is your using a typecast from double to int when you say that n = pow(x,i) and n = 1 + n... Notice that x and i are both double and when you assign them to n, you switch the type from double to int, which results in 2 in C++. Even though, 38 iterations will yield 2 on any programming language...

Code:

```#include <iostream> #include [itex] using namespace std; int main(){   double n = 0;   double x,i;     x = 0.5;   for(i=0; i <=38; i++){         n += pow(x,i);   }   cout << "The sum results in: " << n << "." << endl;   return 0; }```
It takes only 19 iterations to arrive at 2.
• September 20th 2008, 01:22 AM
CaptainBlack
Quote:

Originally Posted by amor_vincit_omnia
I tried both double & long double, and the value is still 2.

You do know that this sum is 2 correct to about 12 significant digits, don't you? If you work in doubles, and output the result to the full prescission of a double you should see the difference.

RonL
• September 20th 2008, 01:27 AM
CaptainBlack
Quote:

Originally Posted by Aryth
Your problem is your using a typecast from double to int when you say that n = pow(x,i) and n = 1 + n... Notice that x and i are both double and when you assign them to n, you switch the type from double to int, which results in 2 in C++. Even though, 38 iterations will yield 2 on any programming language..

No it won't, the precision of doubles is sufficient to detect the difference between 2 and the sum of the 39 terms in the series and will do so for any language/hardware configuration implementing IEEE floating point.

None of which alters my earlier statement that you should not use the pow function to do this.

RonL