Saturday, May 22, 2010

C++ programing to compute e^(x) and sin(x)?

for this program i'm required to use function...so here is what i have so far...





#include "stdafx.h"


#include %26lt;iostream%26gt;


#include %26lt;iomanip%26gt;


#include %26lt;math.h%26gt;


#include %26lt;string%26gt;


#include "stdlib.h"


using namespace System;


using namespace std;





double EtoX(double, double, int%26amp;);


double SineX(int x, int n);


_int64 Sentinel( );





int main()


{


int n;


double e, sin, ecx, exs;


switch





cout%26lt;%26lt;"enter the value of x, and epsilon"%26lt;%26lt;endl;


cin%26gt;%26gt;x%26gt;%26gt;eps%26gt;%26gt;;


e=EtoX(j);


sin=SineX(g);


cout%26lt;%26lt;"the value of e(x) is= "%26lt;%26lt;setprecision(15)%26lt;%26lt;j%26lt;%26lt;endl;


cout%26lt;%26lt;"the value of sine(x) is= "%26lt;%26lt;setprecision(15)%26lt;%26lt;g%26lt;%26lt;endl;





}





double EtoX(double x, double e, int n)





for(i=0; i%26lt;=n; ++i)


{


e= pow(n,i)/i;








i will greatly appreaciate it if someone can show me how to complete this..cause i'm stuck.

C++ programing to compute e^(x) and sin(x)?
I suppose that what you're trying to do is use the power series for exp(x) by summing up the terms x^n/n! until the next term is less than epsilon, right?





double EtoX(double x, double eps)


{


int n = 0;


denom = 1;


double sum = 1;


double increment = 1;


{


n += 1;


denom *= n;


increment *= x;


sum += increment/denom;


} while ( increment %26gt; eps );


return sum;


}





However, you should put a counter in there so that you'll exit if some dummy puts in eps=0 to avoid an infinite loop.
Reply:If what you intend to do is e^x and sin(x), math.h library has the "exp" and "sin" functions that will do it for you. The usage is something like:


exp(x) //where x is a double or float variable


sin(x) //where x is a double or float variable in radians.
Reply:should be:





math.e^ * x


math.sin * x


No comments:

Post a Comment