?
=?ISO-8859-1?Q?Martin_J=F8rgensen?=
Hi,
Consider (factorial.cpp):
#include <iostream>
using namespace std;
double R=3.2; /* not used, but R is static because it is a global
variable (file scope) */
int F(int n); /* prototype */
int main()
{
int number;
cout << "Enter an integer of which to find the factorial of: ";
cin >> number;
cout << "Let's see what factorial(" << number << ") gives us. It gives
us: " << F(number) << endl;
}
int F(int i) /* automatic storage variable -> allocated in a stack (only
exists during execution of this function) */
{
static int count = 0; /* count on the other hand is static (explicitly)
-> exists during the whole program run */
++count; /* increment counter */
if (i==0)
{
cout << "Count = " << count << endl << endl;
return 1;
}
else
return i*F(--i);
}
I'm wondering why it gives me 0 as output (after count has been printed
to the screen)... Also: I'm not sure how to print out the correct
factorial value...
Best regards / Med venlig hilsen
Martin Jørgensen
Consider (factorial.cpp):
#include <iostream>
using namespace std;
double R=3.2; /* not used, but R is static because it is a global
variable (file scope) */
int F(int n); /* prototype */
int main()
{
int number;
cout << "Enter an integer of which to find the factorial of: ";
cin >> number;
cout << "Let's see what factorial(" << number << ") gives us. It gives
us: " << F(number) << endl;
}
int F(int i) /* automatic storage variable -> allocated in a stack (only
exists during execution of this function) */
{
static int count = 0; /* count on the other hand is static (explicitly)
-> exists during the whole program run */
++count; /* increment counter */
if (i==0)
{
cout << "Count = " << count << endl << endl;
return 1;
}
else
return i*F(--i);
}
I'm wondering why it gives me 0 as output (after count has been printed
to the screen)... Also: I'm not sure how to print out the correct
factorial value...
Best regards / Med venlig hilsen
Martin Jørgensen