Newbie here...needs help

• 06-29-2003
Diceman
Newbie here...needs help
Hey guys, Im new at this C++ programming. I have a problem with the Homework assignment, can anyone shed some light on this
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 have to use a while structure
thank you so much in advance fellas
• 06-29-2003
Hammer
What bit are you having trouble with? post the code you've written so far, and someone will guide you.

• 06-29-2003
Diceman
I'm not even sure how to even start with this program. The question in the first thread is the question that I was assigned. I do not have a clue where to start.
i'm having a real hard time with c++
I wish someone lived down here on the Mississippi Gulf coast who could tutor me.
• 06-29-2003
Hammer
There's plenty of tutorials around on the web, including one on this site. Also, get yourself a good book (do a forum search to find recommendations).

Once you have written some code, people here will be more than willing to help you with it.

Here's a starter
Code:

```#include <iostream> using namespace std; // Read this int main() {   int myValue;   myValue = 11;   cout <<myValue <<endl; }```
Don't worry if it doesn't all make sense yet, just stick with it, and it will get better :)
• 06-29-2003
Diceman
• 06-29-2003
Zach L.
Consider the problem you are working on. Try to break it down a bit. You know what the overall task is (to compute a series approximation to e), but try to break it into smaller more obtainable tasks.
• 06-30-2003
laasunde
If I was to make suggestion maybe you should consider making a function to calculate 1!, 2!, 3!, 4! etc.
• 07-06-2003
Eric08
I think I have the answer but it maybe wrong, your mathematical constant had to be e=1+1/1+!2/1+!3/1+!4 thats what I came up with, since I continued getting errors when I attempted your problem without the correction. I would post the code, but please show effort.

Another thing my formula is correct, you would get errors division by 0 since 1!+2, therefore you would use my formula e=1+1/1+!2... This is all the help I'm going to give. The rest should be explanitory.
• 07-06-2003
confuted
No, his original formula was correct.

e=1+ 1/(1!)+1/(2!)+1/(3!)+1/(4!)+ ... +1/(infinity!)

For both the loop in your program that calculates e, and the loop in your factorial function, I would suggest you look into something called a "for loop" (Sorry if you already knew that... you seem lost)
• 07-06-2003
Diceman
Here is guys I finally got it.
I do not know how do the codes, that Hammer is refferring to.
Thanks for trying to help.

code:

#include <iostream>
using namespace std;
int num;
int main()
{
cout<<"Please enter a positive number: ";
cin>>num;

if(num<0)
cout<<"Must be a nongegative number: "<<endl;

else
{
double e=1;
for (int i=1;i<=10;i++)
{
double fact=1;
for (num=1;num<=i;num++)
fact=fact*num;
e=e+(1/fact);
}
cout<<"The value of e is: "<<e<<endl;

}

return 0;
}

end code:
• 07-06-2003
mercury7
for (num=1;num<=i;num++)

This defeats the purpose of getting input for 'num'.

Anyway I don't even understand the problem. What in the hell is that 1!+1 stuff anyway? What exactly is it that you are calculating?
Code:

``` #ifndef IOSTREAM #include <iostream> #endif using namespace std; int main() {   int num = -1;   while(num<0)  {   cout<<"Please enter a positive number: " << endl;     cin>>num;           if(num<0 || !cin.good()) // edit   {     cout << "number must be non-negative." << endl;   }  }    // no clue here...    double e=1;     for (int i=1;i<=10;i++)  {   double fact=1;         for (;num<=i;num++)     {         fact=fact*num;                 e=e+(1/fact);     }  // ... // }   cout<<"The value of e is: "<<e<<endl;  return e; }```
• 07-07-2003
bennyandthejets
I think the idea here may be to create a program that allows you to run a certain number of iterations to get an approximation of the value of e. You would have to choose how many iterations to run. The more you run, the slower it is, but the more accurate it is. I would create a function first that finds the factorial of a given number. That shouldn't be too hard. Then I would create a loop that ascends through positive integers, running the formula with each factorial. Here's an example:

Code:

```double factorial(int num) {     double count;     count =1;     for (int i=1;i<=num;i++)     {         count*=i;     }     return count; } int main() {     int nIterations=20;     double tempE=1;     for (int i=1;i<=nIterations;i++)     {           tempE+=1/factorial(i);     }     cout << tempE << endl;     return 0; }```
I'm not sure about the math really, but that should give you an approximation that's a bit less than the actual value. There may be some issues with factorial sizes, they can get quite big, but I think it's all good.

An improvement could be to keep going through iterations and displaying the result until the user presses a key, that would allow them to stop when accurate enough.

I hope this helps.
• 07-07-2003
bennyandthejets
Oops, just reread the thread and realized the problem was already solved. Good work Diceman. Something else we could have all done better was to save time with the factorials. Why calculate 5! from the beginning if we just did 4! ? Just keep the factorial variable from one iteration to the next, and multiply by the next value. Eg:

Code:

```double factorial; for (...) { factorial*=i; e+=1/factorial; }```
This would save a lot of time, allowing for more accuracy.