Repetitive Zero Terms

Hi,

I'm trying to write a simple C++ program to display the 1st 4 terms of a series as follows:

Ans = 1 - x^2/2! + x^4/4! - x^5/5!

The intended output is: 1 - number + number - number

The following is the code that I compiled:

#include <iostream>
using namespace std;

int factorial(int n)
{
if(n>1){
n=n*factorial(n-1);
}
else n=1;

return(n);
}

int main(){

int x,i,ans;

cout<<"Determine the expansion of:";
cin>>x;

cout<<"Ans = 1";

for(i=1;i<4;i++){

ans=((-1)^(i))*(x^(2*i))/factorial(2*i);

if(i=1,3)
cout<<"+"<<abs(ans);
else
cout<<"-"<<abs(ans);

}


return (0);
}

The problem is that everytime I run the compile program the output appears to be 1+0+0+0+0....all the way to infinity (I have to manually close the command prompt window).
I've looked over the code and can't seem to pinpoint what's causing this to happen. I'd be really grateful if someone could help me

Thanks,
Learner82

You're using integers, and the division is truncated to zero. Try using double or float instead.
Also if(i=1,3) is always going to run no matter what.
Also the ^ in the answer formula should be replaced by the pow() functon which requires the
<cmath> header file. .......or just i*i*i*i......etc.
Also you haven`t included the first term in answer as far as i can see. Need ans+=.....etc.
Hi,

Thanks for the prompt replies!

By the way the actual expansion I was aiming for was:
Ans = 1 - x^2/2! + x^4/4! - x^8/8!

Also if(i=1,3) should have been if(i=1||3)

Sorry for the typos

Thanks again,
Learner82
Last edited on
Still wrong.

if( i = 1 || 3 )

is wrong for two reasons.

First, 1 || 3 is true, since || is a logical-OR, and both 1 and 3, when treated as booleans are true. So true or true is true.

So the line of code is really

if( i = true )

which is also always true, since = is assignment, not comparison, and the value of ( i = true ) is true, since the value of any expression ( x = y ) is y in C++ for any POD-type.

So your line of code is really

if( true )

Ok this seems to work:

#include <iostream>
using namespace std;

int power(int m,int n){

int j;

for(j=1;j<n+1;j++){
m*=m;
}

return (m);
}

int factorial(int n)
{
if(n>1){
n=n*factorial(n-1);
}
else n=1;

return(n);
}

int main(){

float x,i,ans;

cout<<"Determine the expansion of:";
cin>>x;

cout<<"Ans = 1";

ans=0;

for(i=1;i<4;i++){

ans=(power(-1,i))*(power(x,(2*i)))/factorial(2*i);

if(i==1)
cout<<"+"<<abs(ans);
else if(i==3)
cout<<"+"<<abs(ans);
else
cout<<"-"<<abs(ans);
}
return (0);
}

Thanks for the explanation on the boolean, is there a way to combine if(i==either 1 or 3) in terms of the value of i instead of splitting it into if and else if as I did?

Thanks,
Learner82
You can use the ||, but you can't make it that sort:

1
2
3
if(i == 1 || i == 3) {
//...
}


Although this won't be a problem for this code...you might want to use the built-in pow() instead of your own power function so you can do roots/decimal powers like 2^3.2.
Thanks for the help everyone
Topic archived. No new replies allowed.