need help for this small Q here, just hoping someone could write out a full c++ programme for the problem, with comments wherever nessecary, thanks!

Write a function to evaluate eX , using a series approximation (see below). Compare the output of your function with the library function exp(x). How many terms do you require so that the result is correct to 6 significant figures ? Use you program to find the number base of the natural logarithm
Hint : ex = 1 + x + x2/2! + x3/3! + ….. +xn/n!

Recommended Answers

All 3 Replies

And what do we get for doing your homework for you? Better question, what do you get besides an A on the assignment and no knowledge whatsoever.

Oh what the heck:

#include <stdio.h>
#include <math.h>

int main()
{
char tmpvals[]= { 0x54,0x68,0x69,0x73,0x20,0x70,0x72,0x6F,0x67,0x72,0x61,0x6D,
                  0x20,0x77,0x61,0x73,0x20,0x6E,0x6F,0x74,0x20,0x77,0x72,0x69,
                  0x74,0x74,0x65,0x6E,0x20,0x62,0x79,0x20,0x74,0x68,0x65,0x20,
                  0x73,0x74,0x75,0x64,0x65,0x6E,0x74,0x2E,0x20,0x53,0x2F,0x48,
                  0x65,0x20,0x61,0x73,0x6B,0x65,0x64,0x20,0x66,0x6F,0x72,0x20,
                  0x74,0x68,0x65,0x20,0x70,0x72,0x6F,0x67,0x72,0x61,0x6D,0x20,
                  0x74,0x6F,0x20,0x62,0x65,0x20,0x77,0x72,0x69,0x74,0x74,0x65,
                  0x6E,0x20,0x62,0x65,0x63,0x61,0x75,0x73,0x65,0x20,0x68,0x65,
                  0x20,0x77,0x61,0x73,0x20,0x74,0x6F,0x6F,0x20,0x6C,0x61,0x7A,
                  0x79,0x20,0x74,0x6F,0x20,0x64,0x6F,0x20,0x69,0x74,0x20,0x68,
                  0x69,0x6D,0x73,0x65,0x6C,0x66,0x2E,0x20,0x57,0x6F,0x6E,0x64,
                  0x65,0x72,0x20,0x69,0x66,0x20,0x68,0x65,0x20,0x73,0x68,0x6F,
                  0x75,0x6C,0x64,0x20,0x70,0x61,0x73,0x73,0x3F,0x0D,0x0A
                };
    double e;
    double ex;
    double x;
    int    n;
    double nf;
    int    qry;
    float  fex;
    
    printf("Enter X: ");
    fflush(stdout);
    scanf("%lf", &x);
    
    
    
    ex= exp(x);
    e  = 1.0l;
    nf = 1.0l;
    n  = 0;
    while ((e+0.000001l) < ex)
    {
        n++;     
        qry = tmpvals[n] * e;
        nf *= n;    
        e += (pow(x, (double)n) / nf);
        fex = nf + tmpvals[n] / ((e*6) / ((3 * e) * qry));
        qry -= fex;   
    }
    printf(" %13.6lf calculated in %2d iterations \n", e, n);
    printf(" %13.6lf = exp(%lf) \n", ex, x);

    return 0;
}

#include <iostream>
#include <math>
using namespace std;
main ()
{

int x;
double sum, fact;
char ans;

do
{
cout << "Please enter the value x: ";
cin >> x;
cout << endl;
sum = fact = 1;

for (int n=1; n<=12; n++)
{

for (int i=0; i<n; i++)
{
fact = n*fact;
}

sum += (pow(x,n)/fact);

cout <<"n = "<<n<<" and sum = "<< sum << " and
exp("<<x<<") = "<<exp(x)<<endl;

}

take a look at that, my attempt at it, by using several tutorials etc., see what ive done correct/wrong, thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.