#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    10
    Rep 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. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    33
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    10
    Rep 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?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    33
    Originally Posted by Captain531
    First x and epsilon are inputed from the keyboard .
    I suggest you start here :)
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    10
    Rep Power
    0
    Thanks :D Very helpful!!!
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    10
    Rep Power
    0
    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:
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep 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 05:22 AM.
  14. #8
  15. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Norcross, GA (again)
    Posts
    1,805
    Rep Power
    1570
    @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.

    Comments on this post

    • 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 Short Understanding the C/C++ Preprocessor
    Taming Python A 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
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    10
    Rep 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 :)
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep 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 02:57 PM.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    San Francisco Bay
    Posts
    1,939
    Rep Power
    1313
    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.

IMN logo majestic logo threadwatch logo seochat tools logo