Help with series in C++

Status
Not open for further replies.

hitman050

Journeyman
Can anyone help me with these in C++?

The program is supposed to display solution for series for a given value of n and x. There are two series

1. x - x^3/3! + x^5/5! - x^7/7! + x^9/9! ...... till x^(2n+1)/(2n+1)!

2. 1 - x + x^2/2 - x^3/3 + x^4/4 - x^5/5 ....... till x^n/n

Thanks in advance.
 
Last edited:

redhat

Mad and Furious
i dont know C++, but I can give u the logic behind the code...
after input of values of x and n,
set flag=1
run a loop where the counter is initialised to 1 and incremented by 2 upto (2n+1)
if flag = 1
s=s+((X^counter)/factorial of counter)
flag = 0
else
s=s-((X^counter)/factorial of counter)
flag=1

(where factorial of counter is calculated in another part and called using messages)

2nd series:
same logic as above, but initialise counter at 0, and upto n


ican give u the complete code in JAVA if u wish, but i dont know C++
 

ashfame

Padawan
Code:
#include<iostream.h>
#include<conio.h>

void main()
{ clrscr();
int x,n,sum=0,flag=0;
cout<<"Enter x,n"<<endl;
cin>>x>>n;
for(int i=0;i<n;i=i+1)
{
	if(!flag)
        {
		sum=sum+(x^(2n+1))/factorial(2n+1);
                flag=1;
        }
	else
	{
             	sum=sum-(x^(2n+1))/factorial(2n+1);
                flag=0;
        }
}
cout<<"Sum is "<<sum;
getch();
}

also make a function factorial(int) that will calculate the factorial of argument and return it. Similarly use it to calculate the sum of 2nd series.

P.S. - I don't have any compiler with me. Understand the logic and try yourself
 
Last edited:

ashfame

Padawan
I haven't been taught functions yet. Any other way around the factorial part?

Function is used because we want to calculate factorial in every iteration. It can be done without function too.
Just use a variable to calculate factorial before calculation sum.

like

fact= <calculate using value of i>

sum=sum+.../fact;

I hope I am clear
 

Bandu

Journeyman
@hitman: You might have to first go through the functions part. That comes first when learning modular languages. Will make your life easier. Ofcourse, there is a way around it for the time being.

Original code:
Code:
if(!flag)
	sum=sum+(x^(2n+1))/factorial(2n+1);

New code that does not use factorial:
Code:
int fact, base = 0;
if(!flag)
{
	fact = 1;
	base = 2*n + 1;
	while(base >= 2)
	{
		fact = fact * base - 1;
		base = base - 1;
	}
	sum=sum+(x^(2n+1))/fact;
}

do the same for the else part. And remember that your sum should be a float and not an int.

I do not have C++ compiler myself.
 

ashfame

Padawan
And remember that your sum should be a float and not an int.

Yeps! It should be float else decimal part will be lost everytime and sum will be less than what it should be.

Can you please explain (!flag)?

if flag=0 then !flag=1

we are just using flag to keep check on the thing that series is like, you add one term and then subtract the next term and then add the next term and so on.

I forgot to change the value of flag. Sry for that. Will edit it right away.
 
Last edited:

Bandu

Journeyman
Can you please explain (!flag)?

If your question is about operator !
! is a unary operator - meaning takes only one operand and takes a boolean operand always (atleast in Java. I forgot about C++ and I forgot whether C++ has boolean as a datatype or does it use 0 and 1)

Ex: x is a boolean variable.
x = true;

cout>>x; // Will output true
cout >> !x; // Will output false

Now, don't ask us whats unary and whats boolean. If those are your doubts then you might want to log off and get back to Chapter 1 in your books.

If your question is about (!flag), then the author meant if(flag == false)
if(!flag) is just a short for if(flag == false)

Again, depending on the programming language, you might have to replace false with 0 and true with 1 in above snippets.
 

Count0paw

Right off the assembly line
sin x = x - x^3/3! + x^5/5! - x^7/7! + ...<------- nice topic on C++
been looking for this code but i need ..any one convert this to JAVA....
been Using Jcreator now.. just past by solving perfect number ..this my problem now

in code this to JAVA
sin x = x - x^3/3! + x^5/5! - x^7/7! + ...
 
Status
Not open for further replies.
Top Bottom