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! + ...

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 ...

- 10-10-2004 #1

- 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! + ...

- 10-10-2004 #2

- 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

- 10-11-2004 #3

- 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.

- 10-11-2004 #4

- Join Date
- Sep 2001
- Posts
- 1,398

## 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.

- 10-12-2004 #5

- 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.

- Exactly how to get started with C++ (or C) today
- C Tutorial
- C++ Tutorial
- 5 ways you can learn to program faster
- The 5 Most Common Problems New Programmers Face
- How to set up a compiler
- 8 Common programming Mistakes
- What is C++11?
- Creating a game, from start to finish

- How to create a shared library on Linux with GCC - December 30, 2011
- Enum classes and nullptr in C++11 - November 27, 2011
- Learn about The Hash Table - November 20, 2011
- Rvalue References and Move Semantics in C++11 - November 13, 2011
- C and C++ for Java Programmers - November 5, 2011
- A Gentle Introduction to C++ IO Streams - October 10, 2011