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
Printable View
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!
Code:return std::exp(1);
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.
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.
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.
The thing is what I typed is what he gave us to use. Supposed to make a function out of it? I thought all along it did not make any sense. Is it possible to start all over and how would I start declaring a function to calculate the Euler's number?
I gave it a go with some random number -- fa (13.0) -> output: 2.71828Code:double fa (double x)
{
double tmp1=0, tmp2=0;
for (int j=1; j<=x; ++j)
{
tmp1 = 1.0;
for (int i=1; i<j; ++i)
{
tmp1 *= i;
}
tmp2 += 1.0 / double(tmp1);
}
return tmp2;
}
Thanks for telling me about removing the return it worked YEAH