Anyway can help me to solve this problem???

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2005
Posts: 14
Reputation: kyakobi84 is an unknown quantity at this point 
Solved Threads: 0
kyakobi84 kyakobi84 is offline Offline
Newbie Poster

Anyway can help me to solve this problem???

 
0
  #1
Oct 11th, 2005
What is the code for ( e^x = 1 + x/1! + x^2/2! + x^3/3! + ..... )

anyway can help me, plzzz :cry: :rolleyes:
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,039
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: Anyway can help me to solve this problem???

 
0
  #2
Oct 11th, 2005
Use a 'for' loop. Show what you think works, and tell us exactly what each line does and why.

In doing this, you'll probably solve your problem, but if that fails, you'll have people here willing to help you.
All my posts may be redistributed under the GNU Free Documentation License.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 4
Reputation: alloy_zeus is an unknown quantity at this point 
Solved Threads: 0
alloy_zeus's Avatar
alloy_zeus alloy_zeus is offline Offline
Newbie Poster

Re: Anyway can help me to solve this problem???

 
0
  #3
Oct 14th, 2005
Are you trying to just get the natural exponential of a number?
There's a function in math library, if I'm not wrong.
Try to see if the math.h of your compiler has a double exp (double) function.

See
http://unixhelp.ed.ac.uk/CGI/man-cgi?exp+3

For microsoft, the function prototype may be different:

http://msdn.microsoft.com/library/de...larray_exp.asp
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 14
Reputation: kyakobi84 is an unknown quantity at this point 
Solved Threads: 0
kyakobi84 kyakobi84 is offline Offline
Newbie Poster

Re: Anyway can help me to solve this problem???

 
0
  #4
Oct 14th, 2005
thanks for replyiing me,

But i wanna write the code for (e^x = 1 + x/1! + x^2/2! + x^3/3! + ..... ) without using math library.
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 14
Reputation: kyakobi84 is an unknown quantity at this point 
Solved Threads: 0
kyakobi84 kyakobi84 is offline Offline
Newbie Poster

Re: Anyway can help me to solve this problem???

 
0
  #5
Oct 14th, 2005
#include<iostream.H>

int main()
{

//(e^x = 1 + x/1! + x^2/2! + x^3/3! + .....)

int result = 1;
int x = 2;
int z = 1;
int e = 1;

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

	while ( z != 0)
	{
	result *= z;
	z--;
	}

	//cout << "Result = " << result << endl;
	//cout << "Z = " << z << endl;
	
	e = e + x^i/result;

	z++;
}

cout << "The Sum = " << e << endl;

return 0;
}
<< moderator edit: added code tags: [code][/code] >>


The Bold font is the factorial part & its not working well, i need to fix tht, plz help (i dont wanna use any function)[I][U]
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 80
Reputation: Daishi is an unknown quantity at this point 
Solved Threads: 2
Daishi Daishi is offline Offline
Junior Poster in Training

Re: Anyway can help me to solve this problem???

 
0
  #6
Oct 14th, 2005
I think you know how to take a factorial of a number, but your variable usage is not good.

  1. while ( z != 0)
  2. {
  3. result *= z;
  4. z--;
  5. }

Now, honestly, that works if you first set z to the value of whatever you want to take the factorial of, and set result to one. You're setting z equal to one before the for loop, and then incrementing it at the end of each loop, but you decrement it to zero before that, so your while loop is *entirely pointless*.

Just multiply result by i, you don't need a while/for loop or whatever.

-Fredric
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 14
Reputation: kyakobi84 is an unknown quantity at this point 
Solved Threads: 0
kyakobi84 kyakobi84 is offline Offline
Newbie Poster

Re: Anyway can help me to solve this problem???

 
0
  #7
Oct 14th, 2005
  1. #include<iostream.H>
  2.  
  3. int main()
  4. {
  5.  
  6. //(e^x = 1 + x/1! + x^2/2! + x^3/3! + .....)
  7.  
  8. int result = 1;
  9. int x = 2;
  10. int e = 1;
  11.  
  12. for (int i = 1 ; i < 4 ; i++)
  13. {
  14.  
  15. result *= i;
  16.  
  17. //cout << "Result = " << result << endl;
  18.  
  19. e = e + x^i/result;
  20.  
  21. }
  22.  
  23. cout << "The Sum = " << e << endl;
  24.  
  25. return 0;
  26. }
<< moderator edit: added [code][/code] tags >>




You mean like this???
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 80
Reputation: Daishi is an unknown quantity at this point 
Solved Threads: 2
Daishi Daishi is offline Offline
Junior Poster in Training

Re: Anyway can help me to solve this problem???

 
0
  #8
Oct 14th, 2005
I didn't see it the first time, but the ^ doesn't do what you think it does in C++, if you want to take m to the n power then you use the pow function like so...

  1. #include <math.h>
  2.  
  3. ...
  4.  
  5. double m=2.0, n=3.0, result;
  6. result = pow(m, n);

That would return 2 to the 3rd, or 8. Hope that helps a bit.

-Fredric
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 4
Reputation: alloy_zeus is an unknown quantity at this point 
Solved Threads: 0
alloy_zeus's Avatar
alloy_zeus alloy_zeus is offline Offline
Newbie Poster

Re: Anyway can help me to solve this problem???

 
0
  #9
Oct 18th, 2005
Hi, I have some issues with your variable names. result is actually not the result, and e is actually the natural log constant in your comments. Made some changes to the var and trying to do away with the pow fn.
Not sure if you want to restrict your input to just integral values, but output will most definitely be double?

  1. int main()
  2. {
  3.  
  4. //(e^x = 1 + x/1! + x^2/2! + x^3/3! + .....)
  5.  
  6. double x = 2.0; // input value
  7.  
  8. int factorial = 1;
  9. double pow_x = 1.0;
  10. double result = 1.0;
  11.  
  12. cout << "e^" << x << " = 1";
  13.  
  14. for (int i = 1 ; i < 4 ; i++)
  15. {
  16.  
  17. factorial *= i;
  18. pow_x *= x;
  19.  
  20. cout << " + " << pow_x << "/" << factorial;
  21.  
  22. result = result + pow_x/factorial;
  23.  
  24. }
  25.  
  26. cout << endl << "= " << result << endl;
  27.  
  28. return 0;
  29. }

I've not tried the code, but should be ok ...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC