Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
November 18th, 2012, 06:49 AM
 Captain531
Registered User

Join Date: Nov 2012
Posts: 10
Time spent in forums: 3 h 23 m 14 sec
Reputation Power: 0
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 .

#2
November 18th, 2012, 09:42 AM
 bdb
Contributing User

Join Date: Aug 2012
Posts: 156
Time spent in forums: 1 Week 15 h 48 m 11 sec
Reputation Power: 33

#3
November 18th, 2012, 11:33 AM
 Captain531
Registered User

Join Date: Nov 2012
Posts: 10
Time spent in forums: 3 h 23 m 14 sec
Reputation Power: 0
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?

#4
November 18th, 2012, 12:57 PM
 bdb
Contributing User

Join Date: Aug 2012
Posts: 156
Time spent in forums: 1 Week 15 h 48 m 11 sec
Reputation Power: 33
Quote:
 Originally Posted by Captain531 First x and epsilon are inputed from the keyboard .

I suggest you start here

#5
November 18th, 2012, 01:15 PM
 Captain531
Registered User

Join Date: Nov 2012
Posts: 10
Time spent in forums: 3 h 23 m 14 sec
Reputation Power: 0

#6
November 20th, 2012, 02:55 AM
 Captain531
Registered User

Join Date: Nov 2012
Posts: 10
Time spent in forums: 3 h 23 m 14 sec
Reputation Power: 0
I was sarcastic and im sorry for that

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

#7
November 20th, 2012, 05:40 AM
 bdb
Contributing User

Join Date: Aug 2012
Posts: 156
Time spent in forums: 1 Week 15 h 48 m 11 sec
Reputation Power: 33
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 floating-point 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 06:22 AM.

#8
November 20th, 2012, 10:01 AM
 Schol-R-LEA
Commie Mutant Traitor

Join Date: Jun 2004
Location: Norcross, GA (again)
Posts: 1,799
Time spent in forums: 1 Month 2 Weeks 3 Days 2 h 1 m 23 sec
Reputation Power: 1569
@bdb: the "stdafx.h" header is a Microsoft specific 'pre-compiled' header containing header information matching for Microsoft Foundation Classes. Similarly, the _tmain() function is an MFC-specific replacement for main(), which includes extra built-in information necessary for MFC-based programs. As you point out, this isn't actually an MFC program, so neither of them are actually needed.
bdb agrees: Thank you very much for the comprehensive answer.
__________________
Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
#define KINSEY (rand() % 7) λ Scheme is the Red Pill
Scheme in ShortUnderstanding the C/C++ Preprocessor
Taming PythonA Highly Opinionated Review of Programming Languages for the Novice, v1.1

FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. \$4500 obo. tverres@et.ins.gov

#9
November 20th, 2012, 02:44 PM
 Captain531
Registered User

Join Date: Nov 2012
Posts: 10
Time spent in forums: 3 h 23 m 14 sec
Reputation Power: 0
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

#10
November 20th, 2012, 03:54 PM
 bdb
Contributing User

Join Date: Aug 2012
Posts: 156
Time spent in forums: 1 Week 15 h 48 m 11 sec
Reputation Power: 33
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 03:57 PM.

#11
November 20th, 2012, 11:02 PM
 Lux Perpetua
Contributing User

Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,939
Time spent in forums: 1 Month 1 Week 3 h 27 m 29 sec
Reputation Power: 1312
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.

 Viewing: Dev Shed Forums > Programming Languages > C Programming > New to programming and i need a little help