Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 18-08-2008, 05:02 PM   #1 (permalink)
Broken In
 
Join Date: Sep 2006
Posts: 128
Default Help with series in C++


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 by hitman050; 18-08-2008 at 05:43 PM.
hitman050 is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 18-08-2008, 05:18 PM   #2 (permalink)
18 Till I Die............
 
Join Date: Jul 2004
Location: India, Mumbai, Marine Lines
Posts: 5,792
Default Re: Help with series in C++

Aren't they limits of sin x and e^(-x)
__________________
http://www.bash.org/?258908
mehulved is offline  
Old 18-08-2008, 05:36 PM   #3 (permalink)
Broken In
 
Join Date: Sep 2006
Posts: 128
Default Re: Help with series in C++

No idea about that. But I need to know how to display the series for n.
hitman050 is offline  
Old 18-08-2008, 07:54 PM   #4 (permalink)
Mad and Furious
 
redhat's Avatar
 
Join Date: May 2006
Location: Visual Basic 6.0
Posts: 453
Default Re: Help with series in C++

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++
__________________
My new Tech Blog : http://technewspaper.blogspot.com/
redhat is offline  
Old 18-08-2008, 08:07 PM   #5 (permalink)
Alpha Geek
 
ashfame's Avatar
 
Join Date: Mar 2006
Location: Delhi / Jaipur
Posts: 765
Default Re: Help with series in C++

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
__________________
My RIG: C2D 2.0Ghz + Intel965 Mobo + OCZ Matched Pair 2X1GB DDR2 800Mhz + LG L226WTQ 22" LCD + CM 500W PSU + Palit HD 4850 512GDDR3
HOMEPAGE : http://www.ashfame.com | BLOG : http://blog.ashfame.com

Last edited by ashfame; 18-08-2008 at 08:59 PM.
ashfame is offline  
Old 18-08-2008, 08:13 PM   #6 (permalink)
Broken In
 
Join Date: Sep 2006
Posts: 128
Default Re: Help with series in C++

I haven't been taught functions yet. Any other way around the factorial part?
hitman050 is offline  
Old 18-08-2008, 08:33 PM   #7 (permalink)
In The Zone
 
Join Date: Sep 2005
Posts: 227
Default Re: Help with series in C++

You want to create 2 for loops, one nested inside the other.
Try it, should work.
__________________
Our deepest fear is not that we are inadequate Our deepest fear is that we are powerful beyond measure
karmanya is offline  
Old 18-08-2008, 08:36 PM   #8 (permalink)
Alpha Geek
 
ashfame's Avatar
 
Join Date: Mar 2006
Location: Delhi / Jaipur
Posts: 765
Default Re: Help with series in C++

Quote:
Originally Posted by hitman050 View Post
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
__________________
My RIG: C2D 2.0Ghz + Intel965 Mobo + OCZ Matched Pair 2X1GB DDR2 800Mhz + LG L226WTQ 22" LCD + CM 500W PSU + Palit HD 4850 512GDDR3
HOMEPAGE : http://www.ashfame.com | BLOG : http://blog.ashfame.com
ashfame is offline  
Old 18-08-2008, 08:39 PM   #9 (permalink)
Broken In
 
Join Date: Sep 2006
Posts: 128
Default Re: Help with series in C++

I will try it.

I am not aware of logical operator '!'. What does it do?
hitman050 is offline  
Old 18-08-2008, 08:48 PM   #10 (permalink)
Broken In
 
Join Date: Aug 2008
Location: Mumbai, India
Posts: 169
Default Re: Help with series in C++

@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.
Bandu is offline  
Old 18-08-2008, 08:49 PM   #11 (permalink)
Alpha Geek
 
ashfame's Avatar
 
Join Date: Mar 2006
Location: Delhi / Jaipur
Posts: 765
Default Re: Help with series in C++

Quote:
Originally Posted by hitman050 View Post
I will try it.

I am not aware of logical operator '!'. What does it do?
It just inverts the operand.
0 -> 1
1 -> 0
__________________
My RIG: C2D 2.0Ghz + Intel965 Mobo + OCZ Matched Pair 2X1GB DDR2 800Mhz + LG L226WTQ 22" LCD + CM 500W PSU + Palit HD 4850 512GDDR3
HOMEPAGE : http://www.ashfame.com | BLOG : http://blog.ashfame.com
ashfame is offline  
Old 18-08-2008, 08:50 PM   #12 (permalink)
Broken In
 
Join Date: Sep 2006
Posts: 128
Default Re: Help with series in C++

Can you please explain (!flag)?
hitman050 is offline  
Old 18-08-2008, 08:50 PM   #13 (permalink)
Alpha Geek
 
ashfame's Avatar
 
Join Date: Mar 2006
Location: Delhi / Jaipur
Posts: 765
Default Re: Help with series in C++

Quote:
Originally Posted by Bandu View Post
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.

Quote:
Originally Posted by hitman050 View Post
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.
__________________
My RIG: C2D 2.0Ghz + Intel965 Mobo + OCZ Matched Pair 2X1GB DDR2 800Mhz + LG L226WTQ 22" LCD + CM 500W PSU + Palit HD 4850 512GDDR3
HOMEPAGE : http://www.ashfame.com | BLOG : http://blog.ashfame.com

Last edited by ashfame; 18-08-2008 at 08:58 PM. Reason: Automerged Doublepost
ashfame is offline  
Old 18-08-2008, 08:59 PM   #14 (permalink)
Broken In
 
Join Date: Aug 2008
Location: Mumbai, India
Posts: 169
Default Re: Help with series in C++

Quote:
Originally Posted by hitman050 View Post
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.
Bandu is offline  
Old 02-10-2008, 10:49 PM   #15 (permalink)
Right Off the Assembly Line
 
Join Date: Oct 2008
Posts: 1
Default Re: Help with series in C++

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! + ...
Count0paw is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Nokia Xpress Music Series Vs. SE Walkman Series just4fun Mobiles and Tablets 29 12-09-2007 10:50 PM
nokia n series vs sony ericsson w series... ssk_the_gr8 Mobiles and Tablets 23 11-09-2007 09:30 PM
whats the difference b/w K series and W series ? saurabh kakkar Mobiles and Tablets 1 29-07-2007 09:32 PM
Series 60 v2 vs Series 60 v3 skirix Mobiles and Tablets 5 29-06-2007 11:32 AM
ati 9800 series vs ati x1600 series vish786 Hardware Q&A 5 18-12-2006 12:48 AM

 
Latest Threads
- by Sujeet
- by clmlbx
- by Sujeet
- by icebags

Advertisement




All times are GMT +5.5. The time now is 11:10 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2