Thread: small program to calculate e

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    241

    small program to calculate e

    the program goes like this:
    it should calculate the value of e^x by the series
    1+x+x^2/2!+x^3/3!...+x^n/n!
    when the user inputs a value , eps , which the program has to stop compute e^x when x^n/n<eps.

    I wrote 2 function , one calculating the factorial result (fact), and one calculates the value x^n/n! (monom).
    these functions works well, but the program keeps giving me 1 as result for e^x (or it gets stuck..)
    it seems that the problem comes from the condition monom (x,j)<eps, but I can't see why...
    Code:
    #include <stdio.h>
    #include <math.h>
    
    float fact (int);
    double monom (float x, int n);
    
    int main (void)
    {
    float x;
    printf ("please enter x value for e^x: ");
    scanf ("%f", &x); 
    double exp=1;
    float eps;
    printf ("please enter delta: ");
    scanf ("%f", &eps);
    int j;
    for (j=1; monom (x,j)<eps; j++)
    exp+=monom (x,j);
    printf ("%f",exp);
     
    return 0;
    }
    
    float fact (int a)
    {    
    float result=1;
    int i;
    for (i=1;i<=a;i++)
    result*=i;    
    return result;    
    }
    
    double monom (float x, int n)
    {
    return (pow(x,n)/fact(n));    
    }
    can you guys help me? thanks a lot!

  2. #2
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Don't you want your loop to terminate once the difference of the successive terms in the series becomes less than epsilon rather than greater than?

    Also it seems rather inconsistent that you name your variable eps (suggesting epsilon) and then ask the user to enter delta .

    Otherwise though the code is quite good, you need to work on your indentation though and personally I'm not a fan of declaring my variables throughout my code but to each his own.

  3. #3
    Registered User
    Join Date
    Dec 2013
    Posts
    241
    Hi deadPlanet , isn't what you suggesting is what the program does?
    I want to stop the loop when x^n/n!<eps, eps is the amount of accuracy we want for e^x.
    how would you rephrase the condition? (I admit I don't quit get your thought..)

    edit:
    well, I chang the < to be > and it worked , I guess I got confused with the middle component of the for loop. the middle component is actually "do it while.." and not "do it until" and that what got me confused ... beginners things ..
    thank you very much!
    Last edited by Dave11; 12-27-2013 at 05:05 AM.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Also notice the name "exp" is already used by the standard library, so using that as a variable name is not a good idea:

    Man Page for exp (freebsd Section 3) - The UNIX and Linux Forums

  5. #5
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by DeadPlanet View Post
    Otherwise though the code is quite good.
    Serious question, what makes it good? To be honest (sorry Dave11, you're learning so I understand) I can't find much good about it.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Quote Originally Posted by Hodor View Post
    Serious question, what makes it good? To be honest (sorry Dave11, you're learning so I understand) I can't find much good about it.
    The code is not unnecessarily long. All functions and variables have close to reasonable names. Functions are correctly prototyped. Proper standard header files are included. We even have int main( void ) and return 0. These are all things that lots of learning programmers struggle with.

    This is not production code and I won't be including it in my own projects but Dave11 is very much on the right track. One of the major obstacles that stand in the way of him becoming a good programmer is quitting too easily. I'm of the belief that encouragement gives people some self-belief and so they are less likely to quit.

    I only ever compliment people on things that I think they are actually doing well at (relative to their skill level). I do not just make things up to make people feel good.

    I guess it's more fun to be a dickhead towards people though so we can try your idea of telling people their code is bad from now on.

  7. #7
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by DeadPlanet View Post
    I guess it's more fun to be a dickhead towards people though so we can try your idea of telling people their code is bad from now on.
    If you think I was being a dickhead, then you're mistaken. It was an honest question. I don't mince words (often) but at the same time I did not say anything at all about the programmer; I was referring to the code and my comment (question, actually) was purely objective.

  8. #8
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Code:
    #include <stdio.h>
    #include <math.h>
    
    float fact(int);
    double monom(float x, int n);
    
    int main(void)
    {
        float x;
        printf("please enter x value for e^x: ");
        scanf("%f", &x);
        double exp = 1;
        float eps;
        printf("please enter delta: ");
        scanf("%f", &eps);
        int j;
        for (j = 1; monom(x, j) < eps; j++)
            exp += monom(x, j);
        printf("%f", exp);
    
        return 0;
    }
    
    float fact(int a)
    {
        float result = 1;
        int i;
        for (i = 1; i <= a; i++)
            result *= i;
        return result;
    }
    
    double monom(float x, int n)
    {
        return (pow(x, n) / fact(n));
    }
    I just felt like indenting the code. Ah, much nicer to look at.

    On a serious note, Dave11, do you have access to a debugger, where you can step through your program while it is running, and examine your variables to see what is going on? This can be a very instructive thing to do sometimes.
    Last edited by kermit; 12-27-2013 at 08:43 AM.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Your for loop condition is both unnecessarily inefficient, and incorrect.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Registered User
    Join Date
    Dec 2013
    Posts
    241
    hi all!
    I posted this code after many attempts to make it work , so I kind of abandoned the indentation at some point, usually I DO write the code with indentation.
    about the delta-epsilon issue - this is an exercise for me, so I didn't put much thought on the output text..

    kermit: I use dev-c++ , so I do have excess to debugger, but this program has good syntax , but bad logic, so debugger wasn't helpful at the time
    thanks all for your comments ! I will surly post more questions as they come!
    Last edited by Dave11; 12-28-2013 at 09:24 AM.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Dave11 View Post
    kermit: I use dev-c++ , so I do have excess to debugger, but this program has good syntax , but bad logic, so debugger wasn't helpful at the time
    That is an interesting statement, as a debugger is useless for syntax errors but excellent for logic errors. I hope you're not thinking of the warnings you get from the compiler as your debugger, because it isn't. A debugger is a program (like gdb) that allows you to step through a program's execution, one step at a time, which will allow you to see when and how your logic errors manifest themselves.

  12. #12
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    I recommend using a more recent IDE than Dev-C++. The compiler it comes with is very out of date.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  13. #13
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by King Mir View Post
    I recommend using a more recent IDE than Dev-C++. The compiler it comes with is very out of date.
    At the very least make sure it is the most up to date version. There is a new (forked) version of Dev-C++. You can find it here

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to calculate the program running took?
    By DaniiChris in forum C Programming
    Replies: 17
    Last Post: 07-07-2008, 01:56 PM
  2. Need help with small program to calculate a salary
    By Guti14 in forum C++ Programming
    Replies: 13
    Last Post: 01-11-2004, 05:54 PM
  3. Small program that has to calculate miles per gallon
    By Guti14 in forum C++ Programming
    Replies: 6
    Last Post: 01-06-2004, 02:47 PM
  4. Program to Calculate Pi
    By zowen in forum C++ Programming
    Replies: 1
    Last Post: 10-11-2003, 11:18 AM
  5. A small problem with a small program
    By Wetling in forum C Programming
    Replies: 7
    Last Post: 03-25-2002, 09:45 PM