I need help on writing a C++ program to compute Euler's number it being
e= 1/0! + 1/1! + 1/2! + ... + 1/n!
Please help I am new at the programming scene I'd greatly appreciate any feedback
I need help on writing a C++ program to compute Euler's number it being
e= 1/0! + 1/1! + 1/2! + ... + 1/n!
Please help I am new at the programming scene I'd greatly appreciate any feedback
Seems like homework to me. Tell us what you've done, what you think you can do, pseudo-code, etc.
Its not particularly difficult, you need two functions: 1 - something to compute a factorial (simple), and 2 - something to compute the number. I wrote a program that should be working, but for some reason my computer's rounding everything to ints before it adds them =(
I'm a bit confused why a float var type would be becoming an int ?
Good luck!
"Anyone can aspire to greatness if they try hard enough."
- Me
Code:return std::exp(1);
"If you tell the truth, you don't have to remember anything"
-Mark Twain
No real need to write a factorial function. In fact doing so would probably slow it down.
When doing this problem the first thing to realize is that n will always be greater then or equal to 0. As such 1/0! will always be done. So it is a good idea to initialize based off of that.
So we create a double to store e and give it the value of 1.0 and create an unsigned int to store the denominator and initialize it to 1 (0! is defined to 1 for this problem).
Walk though the steps for n=3:
n=0: e=1.0 d=1
n=1:
d = d * 1 (1! is 1 * 0!)
e = e + 1.0 / d
n=2:
d = d * 2 (2! is 2 * 1!)
e = e + 1.0 / d
n=3:
d = d * 3 (3! is 3 * 2!)
e = e + 1.0 / d
The higher the value of n the more precision you gain.
I hope you can see now the loop in this problem.
So now I just put in a while for some n value?
see attachment
I can't open attachments on this computer, and I'm sure some people don't feel like clicking on the link, either. Since it's only 621 bytes you might as well just post it in [code] tags.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
After looking at your code I can see you totally ignored my explanation of the problem.
Let me ask these two questions:
What is the value of e?
Can a variable of type long store the value of e?
> return factX;
> cout << "The Euler's number is " << factX << endl;
Remove the return, it is stopping you printing.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Just think about what you're telling the computer to do:Code:#include <iostream> #include <iomanip> #include <string> #include <cmath> using namespace std; //I would avoid this. If your teacher disagrees, strangle him. int main( ) { int n; //Initializing counter //fine, but for clarity you might want to save it for later long factX = 1; //setting factorial to start at 1 //e is not a type of integer! Use double. n = 0; //setting counter to zero //might as well do this when you declare the variable, might as well within the loop for(n = 1, factX =1; n<= 10; n++) factX = factX * n; return factX; //return to the caller? Uh oh... cout << "The Euler's number is... doesn't matter because this never executes... " << endl; system("pause"); return 0; }
1. Allocate space for an integer. Call it "n".
2. Allocate space for a long integer. Call it factX. Initialize its value to the integral constant, 1.
3. Set n to the integral constant, 0.
4. Set n to 1. Set factX to 1. Until n is not less than or equal to 10, execute the below code, each time checking first to see if n is still less than or equal to 10. Increase n by 1 each time, after the below code is executed.
4a (The "below" code): set the value of factX so that it is equal to the product of itself and n (this will be an integer).
5. Now that the loop is done, get ready to return to the system process that called this program and give it the value we were calling factX. Clean up. End execution.
Is this what you want to program to do? Write out in prose what you want done, so that a computer might understand it. Then make code.
"If you tell the truth, you don't have to remember anything"
-Mark Twain