November 18th, 2012, 05:49 AM

New to programming and i need a little help
Hello everyone, im new to programming and i need a little help.
Write a C program the computes the value e^x by using the formula :
e^x = 1 + (1/1!)*x + (1/2!)*x^2 +...+ (1/n!)*x^n
The sum stops when 1/n!*x^n<=epsilon , 0<epsilon<=1 ;
First x and epsilon are inputed from the keyboard .
November 18th, 2012, 08:42 AM

November 18th, 2012, 10:33 AM

I dont know how to start im very new at this. I was hoping for some pointers .
For example x=2 1/2!*2^2 = 1 and
1/n!*x^n<=epsilon , and the program should stop
because 0<epsilon<=1 is the statement wrong?
November 18th, 2012, 11:57 AM

Originally Posted by Captain531
First x and epsilon are inputed from the keyboard .
I suggest you start here :)
November 18th, 2012, 12:15 PM

Thanks :D Very helpful!!!
November 20th, 2012, 01:55 AM

I was sarcastic and im sorry for that :o
I come up whit this :
Code:
#include "stdafx.h"
#include "math.h"
int _tmain(int argc, _TCHAR* argv[])
{ int n , f ;
float x , ex , total,eps;
f=1;
n=1;
ex=0;
total=1;
printf("x=");scanf("%f", &x);
printf("epsilon(0;1]=");scanf("%f", &eps);
do{
n++;
f *= n ;
ex=pow(x,n)/f ;
total+=ex;
} while(eps<=ex);
printf("e^%.0f=%f\n",x ,total);
return 0;
}
when i input epsilon to for example 0.00001 and for x= 4 and above , it outputs negative number . But for x= 4 and above , epsilon = 0,1 i get a numeber closer to the real value of e^4 :confused: :confused: :confused:
November 20th, 2012, 04:40 AM

First thing that I noticed: float!
Do not use variables of type float. Prefer variables of type double. Calculations with double are not necessarily slower than with float (and may even be faster); C is geared towards double (kinda like geared towards int) and using float forces the compiler to do extra conversions; and floatingpoint literals are of type double.
Zeroth thing I noticed :)
Use the proper syntax for standard headers: #include <math.h>
I don't know what "stdafx.h" is or why you make a program without a main() function.
I've changed that, added #include <stdio.h> and compiled your code on my computer.
Congratulations! It compiled without warnings :)
I verified the same thing happened at my computer and added this line right before the end of the loop
Code:
printf("... n is %d; f is %d; ex is %f; total is %f\n", n, f, ex, total);
When I run the program with x = 4 and epsilon = 0.00001 I get the output
x = 4
epsilon(0;1] = 0.00001
... n is 2; f is 2; ex is 8.000000; total is 9.000000
... n is 3; f is 6; ex is 10.666667; total is 19.666667
... n is 4; f is 24; ex is 10.666667; total is 30.333333
... n is 5; f is 120; ex is 8.533333; total is 38.866667
... n is 6; f is 720; ex is 5.688889; total is 44.555556
... n is 7; f is 5040; ex is 3.250794; total is 47.806349
... n is 8; f is 40320; ex is 1.625397; total is 49.431746
... n is 9; f is 362880; ex is 0.722399; total is 50.154145
... n is 10; f is 3628800; ex is 0.288959; total is 50.443104
... n is 11; f is 39916800; ex is 0.105076; total is 50.548180
... n is 12; f is 479001600; ex is 0.035025; total is 50.583206
... n is 13; f is 1932053504; ex is 0.034734; total is 50.617940
... n is 14; f is 1278945280; ex is 0.209888; total is 50.827828
... n is 15; f is 2004310016; ex is 0.535716; total is 51.363545
... n is 16; f is 2004189184; ex is 2.142995; total is 53.506540
... n is 17; f is 288522240; ex is 59.544350; total is 6.037810
e^4 = 6.037810
And I can immediatly see the error.
How come, at line with n = 14, the previous f * 14 has the same number of digits?????
I'll let you solve the problem :)
HINT: the range of an integer type with 32 bits cannot hold 13!
the range of an integer type with 64 bits cannot hold 21!
the range of an integer type with 128 bits cannot hold 34!
Last edited by bdb; November 20th, 2012 at 05:22 AM.
November 20th, 2012, 09:01 AM

@bdb: the "stdafx.h" header is a Microsoft specific 'precompiled' header containing header information matching for Microsoft Foundation Classes. Similarly, the _tmain() function is an MFCspecific replacement for main(), which includes extra builtin information necessary for MFCbased programs. As you point out, this isn't actually an MFC program, so neither of them are actually needed.
Comments on this post
November 20th, 2012, 01:44 PM

I come up whit somethig else :
Code:
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{ float x, ex, t, eps;
int i;
printf("x=");scanf("%f", &x);
printf("eps(0;1]=");scanf("%f", &eps);
ex = 1;
t = 1;
i = 1;
while ( 1 > 0 )
{
t = t * x / i;
if ( 2.718282 * t < eps ) break;
ex = ex + t;
i = i + 1;
}
printf("e^%f = %f\n", x, ex);
return 0;
}
and i got the real value of e^x :)
November 20th, 2012, 02:54 PM

Yep, that works!
You might want to use double instead of float, and include the header <stdio.h>.
Last edited by bdb; November 20th, 2012 at 02:57 PM.
November 20th, 2012, 10:02 PM

Since you're new to C programming, I strongly advise you to learn to write portable (ANSI/ISO) C code. Put away your nonstandard headers like stdafx.h and don't take them out again for a long time.
Also, just as bdb said, use double instead of float. floats usually only have about 6 digits of accuracy.