Problem with C++ Program

TheJokerV

Weaksauce
Joined
Mar 23, 2007
Messages
81
Please help!! I am trying to make a C++ program that calculates the value to using the e approximation series e = 1 + 1/1! + 1/2! + 1/3! .... When I go to output the value of sumi which should be e-1 the porgram print out inf. Im assumign this means infiniy but From my calc days I know this equation tends to 0 so I really don't know what's the matter. Can anyone help me?

Here's my code:

Code:
#include <iostream.h>

#include <stdlib.h>

using namespace std;



int factorial (int num){

      int fac;

      for(int g = 1; g <= num; g++){

          fac *= g;

      }

      return fac;

}



int main()

{

      double sumi;

      for(int j = 1; j < 11; j++) {

              for(int i = 1; i < j*10000; i++) {

		sumi += (static_cast<double>(1)/factorial(i));

              }

      cout << "For an estimation of i = "<< j*10000 << " e equals " << 1 + sumi << endl;

      sumi=0;

      }



      system("PAUSE");

      return 0;

}
 
In C++, variables must be initialized before they are used.
 
sumi is declared as a double. Initialization means to assign a variable an initial value, e.g.
Code:
double sumi = 0;
You'll want to initialize all of your other variables in the same manner. Incidentally, your code does indeed compile without issue, but the variables that you didn't explicitly initialize were assigned "junk" values (i.e. whatever happened to be in the memory that the variables occupied).
 
It doesn't compile fine for me. In fact, the compiler points out all the things I pointed out:

Code:
foo.cpp
c:\foo.cpp(15) : warning C4700: uninitialized local variable 'fac' used
c:\foo.cpp(35) : warning C4701: potentially uninitialized local variable 'sumi' used
c:\foo.cpp(35) : warning C4701: potentially uninitialized local variable 'fac' used
 
OK well I initialized the values and the program is still giving me infinity. Any ideas?
 
What values did you use for your initializers?

What did you notice when you debugged the code?
 
You're probably getting overflow problems from trying to compute such a huge fractorial, is my guess.
 
Many problems.

Your factorial code is wrong because you didn't initialize fac. Assuming the compiler initializes it to zero makes your code wrong because anything * zero = 0. Your factorial always returns 0. 1/0 = infinity (or division by zero). You need to initialize fac to 1.

Your factorial code uses int. int's are usually signed 32 bit. When you fix it from above, the largest number you can get the factorial of without overflowing / wrapping around an int, is 16.
it appears that swapping your factorial code to calculate in doubles MAY work. I'm not sure on the factorial limit for double's though, so you may still overflow them.

For an estimation of i = 10000 e equals 2.7182818284590455
For an estimation of i = 20000 e equals 2.7182818284590455

I did this in java, so the datatypes may not be exactly the same. (I can use your code almost verbatim, static_cast is the only real thing that java doesnt have).
 
Thanks for your help guys. It was a dumb error on my part and thank you everyoen for helping me figure it out. I just have a quick question for mike - What compiler do you use? I got a feeling that my compiler is allowing too many errors to slide and makes my programs do some odd things?
 
Visual C++ 2005. Any version of Visual C++ I've used, from 1.0, would report these errors.
 
don't rely on the compiler to catch them, get in the habit of explicitly initializing every value!
 
In spirit, that's not a bad idea. In practice, it results in inefficient code because people follow such an admonition to the letter rather than to its spirit.

At some point, telling a developer to not rely on their compiler is like telling a mechanic to not rely on their wrench.
 
Visual C++ 2005. Any version of Visual C++ I've used, from 1.0, would report these errors.

For gcc, you have to produce an optimized build (-01 or better) for it to catch these errors. This applies to DevC++ also, since it uses Mingw.

You can also turn on warnings like the following to catch more stuff.
Code:
g++ -Wall -Wextra -Wfloat-equal -Wshadow -Wpointer-arith -Wcast-qual -Wcast-align -Wwrite-strings -Wconversion -fno-nonansi-builtins -Wold-style-cast -Woverloaded-virtual file.cc -o file -O3 -s
 
Back
Top