recursive factorial function.

  • Thread starter =?ISO-8859-1?Q?Martin_J=F8rgensen?=
  • Start date
?

=?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
 
O

osmium

Martin Jørgensen said:
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...

You're making it unnecessarily complicated. There is no need for a static
variable such as count. I suggest you start from scratch with that tidbit in
mind.
 
T

TB

Martin Jørgensen skrev:
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 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);

return i * F(i - 1);

Why? On the second last recursion 'i' will equal 0, and
the statement

return 0 * F(0);

will return 0.

http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.15
 
A

Andrew Koenig

....
return i*F(--i);
I'm wondering why it gives me 0 as output (after count has been printed to
the screen)...

The expression i*F(--i) uses and modifies the value of i between two
sequence points. Therefore its behavior is undefined, and there is no point
in trying to find *any* other bugs in this program until this error is
corrected.
 
?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

TB wrote:
return i * F(i - 1);

Why? On the second last recursion 'i' will equal 0, and
the statement

return 0 * F(0);

will return 0.

Wauw... It works...

So you're saying that with i*F(--i), the i will decrement at the same
time both inside and outside the parenthesis...? It says that a sequence
point could be: "after evaluation of all a function's parameters but
before the first expression within the function is executed (1.9p17)"

So it evaluates the "inner" function first. And then multiplying it with
0 since i decreased... Instead of evaluating i in "the outer function"
first, which is "i * F(something)"...


Best regards / Med venlig hilsen
Martin Jørgensen
 
D

Daniel T.

Martin Jørgensen said:
TB wrote:


Wauw... It works...


So you're saying that with i*F(--i), the i will decrement at the same
time both inside and outside the parenthesis...? It says that a sequence
point could be: "after evaluation of all a function's parameters but
before the first expression within the function is executed (1.9p17)"

So it evaluates the "inner" function first. And then multiplying it with
0 since i decreased... Instead of evaluating i in "the outer function"
first, which is "i * F(something)"...

Your sequence points are fine. Trace through your program (by hand if
you have to) where i == 1. It returns: i * F( --i ); Which *first*
decrements i (to 0) then calls F( 0 ) (which returns 1) *then*
multiplies the return value by i (which was already decremented to 0.)
 
?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

Daniel T. wrote:
-snip-
Your sequence points are fine. Trace through your program (by hand if
you have to) where i == 1. It returns: i * F( --i ); Which *first*
decrements i (to 0) then calls F( 0 ) (which returns 1) *then*
multiplies the return value by i (which was already decremented to 0.)

Eerh, that's exactly my problem... Gotta figure out how to debug
properly using either xcode or gdb under mac os x... And it is causing
me some troubles... But it's piece of cake to debug under visual studio
(but that isn't on this computer I'm sitting at right now)...

But I'll try it out tomorrow...


Best regards / Med venlig hilsen
Martin Jørgensen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top