Please Help Me Understand

My assignment is to write a program that estimates the value of the mathematical constant e by using the formula e = 1 + 1/1! + 1/2! + 1/3! +...

I asked for help from a classmate and they sent me there code so now I have the answer but it doesn't help since I don't understand why it works. Could someone please explain to me what this program does and why it works so that I can figure out how to write it myself.

#include <iostream>
#include <iomanip>
using namespace std;

// Lab2 Exercise 4B
// Find the accuracy of e

#include <iostream>
using namespace std;

int main()
{
int accuracy = 1;
int factorial = 1;
int counter = 1;
double e = 1.0;

cout << "Enter desired accuracy of e: ";
cin >> accuracy;


if (accuracy > 1)
{
while (counter <= (accuracy-1))
{
factorial = factorial * counter;
e = e + (1/static_cast<double>(factorial));
counter = counter + 1;
}
cout << "e is " << e << endl;
}




system ("pause");
return 0;
}



What part about that do you not understand? That's pretty straightforward.
I guess the first thing I don't understand is why the while statement is
while(counter <=(accuracy-1)) and how I would know to come up with that.

Is that statement saying that you want to repeat the e= e + (1/static_cast<double>(factorial)) from 1/1! until you get to the accuracy -1 so if you put in 5 for the accuracy it's going to repeat until it gets to 1/5! and then that is the answer?
The variable 'accuracy' is actually used to determine the number of terms to calculate in the series. You can probably understand it better if you think of this variable as 'terms to calculate' instead.

The while loop condition is counter <= accuracy - 1 because the initialized value of 'e' is 1, which happens to be the first term in the series. If you wanted to find the value of e using 5 terms, your while statement needs to loop 4 times, since the first term(loop) is already defined.
Ok so that makes sense now. I guess my next question is why does factorial = factorial * counter for the formula to get the factorial.
Uh... you know how the factorial is calculated?

Like n! = n* (n-1) * (n-2) * .... * 2?

And you know that multiplications are associative?
And you did see that counter is incremented by 1 in every iteration of the loop?
Yes but the counter is increasing so I would think that factorial = factorial * counter would be for ex:

If you chose 5 then it would be
5! = 5 * 6 * 7... when it needs to be 5! = 5 * 4 * 3 * 2 * 1.
Uh... take a sheet of paper, and try to execute the following code snipped as if you were the computer.

1
2
3
4
5
int factorial = 1;
for(int i=2; i<5; ++i)
{
factorial = factorial *i;
}


The problem seems to be less that you don't understand what's going on, you just seem to not quite "see" what's going on.
Ok I understand now...so if the counter is set to 2 and the factorial is set to 1 then what the program is doing is:

1 = 1 * 2 which = 2
2 = 2 * 3 which = 6
6 = 6* 4

and it stops execution at 4 because it has the loop set to the counter being less than 5.


Yep, and pretty much the same thing happens in your example, glad you understand it now.
Thank you so much. I didn't want to just except the answer and not understand it since I will have to produce hand written programs on my mid-term and final without using a computer or other forms of help.
Topic archived. No new replies allowed.