Help Please

This is a discussion on Help Please within the C++ Programming forums, part of the General Programming Boards category; I have to write a program that computes e^x to an accuracy of .0001. I have no idea how to ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    1

    Help Please

    I have to write a program that computes e^x to an accuracy of .0001. I have no idea how to do this so any help would be greatly appreciated...thanks.
    the equation is:
    ex = 1 + x + x2/2! + x3/3! + ... + xn/n! + ...

  2. #2
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    For decimal places, you want to store your value as a float or double, when calling printf, you'll want a %.4f in there, the ".4" specifies 4 decimal places.

    I'm not sure about your equation, but, similar to a very recent post, which makes me think you guys have a CP104 assignment due soon...

    The method that comes to mind for me is using a for loop that counts from 1 to the number of times you want to multiply (x), and then each time, multiply your total by your base number (e)...

    Good luck

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Break the formula down into workable units and then try to use loops, etc. to put things together.

    result = e^x is the sum of an infinite number of terms

    the first and second terms are special cases.

    each term thereafter is the quotient of a numerator and denominator

    the numerator of each term can be calculated based on a specific pattern

    the denominator of each term can be calculated
    based on a specific pattern.

    calculate each numerator and denominator and hence, term, in sequence and add it to the prior result until the value of the term is neglible compared to the result, based on some criteria.

    round the final answer off to a specified level of accurarcy.

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Lightbulb LOOPS and/or RECURSION?

    Welcome to the board Uconn2111 !

    I'll bet you've just had a lecture or chapter on loops (or maybe recursion).

    Get it working without a loop first... just a few terms. Note that there is no factorial built-into C++. You'll have to do that yourself too.

    If you are still having trouble, post the code that you have so far.

    Just in case you're wondering why you are getting vague answers and general suggestions, see the homework policy and forum guidelines. Also read about code tags before posting any code.

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    52
    Your program sounded like fun so i programmed it.....
    Show us ur program thus far or what you think you should do, that way we can help you rather than tell u.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21