i need help with this program. it's very straightforward but I can't seem to get it working. i'm probably making a silly mistake or something. any help/advise/suggestions would be much appreciated, thanks.

Quote

double exp( double x);

For this exercise, you are to write your own function to compute the exponential. The prototype should be

double myexp(double x);

Implement the algorithm given by the pseudocode above. Do not use the pow function from the math library and do not use a factorial function in your implementation of myexp. Use a defined constant for EPSILON. Make it 0.000001. Be sure that your function handles negative as well as positive values for x. (You can see how many terms your function adds to approximate ex by printing the value of n just before returning from the function.)

In main, provide code that tests your function. It should prompt the user to enter a real number, and then call both your function and the library function to compute the exponential. The value returned by your function and the value returned by the library function are both estimates of the actual value. Neither, in general, is exact. Of course the accuracy of your function can be improved by using a smaller value for EPSILON. A sample run might look as follows:

Enter x: 2.0

exp(x) = 7.389056

myexp(x) = 7.389056

**Pseudocode**for exp function

Quote

1. Let variable term represent a term in the exponential series above.

2. Let variable sum represent an approximation to ex.

3. Let n represent a counter.

4. Let EPSILON denote some small constant (e.g., 0.000001).

5. Set term = 1.0, sum = 0.0, and n = 1.

Loop.

6. sum = sum + term

7. term = term * x / n

8. n = n + 1;

9. If | term | < EPSILON then exit loop (sum ~= ex),

else repeat loop (i.e., go to 6)

here is what I currently have:

#include <stdio.h> #include <stdlib.h> #define EPSILON 0.000001 double myexp(double x); int main () { double x; printf("Enter x: "); scanf("%d", &x); printf("exp(%d) = ", x, myexp(x)); return 0; } double myexp(double x) { double x; double term = 1.0; //represents a term in exponential series double sum = 0; //represents an approximation to e^x int n = 1; //counter LCV while (n <= x) { sum += term; term = term * x / n; n += 1; if (term < EPSILON) break; else sum += term; } return; //return value }

This post has been edited by **iqbalmmz**: 04 April 2011 - 01:27 PM