Velocity Reviews > int fact( int n )

# int fact( int n )

venkatesh.k.desai5@gmail.com
Guest
Posts: n/a

 05-16-2006
Hi All,

I want to know how many times 'Hi' will prints; and why?

#include<stdio.h>

int fact( int n );

main() {

int f;

f = fact( 4 );

printf("\nfact = %d", f);
}

int fact( int n ) {
int fa;

if ( n == 0 ) return 1;

fa = n * fact( n - 1 );

printf("\nHi");

return fa;
}

jacob navia
Guest
Posts: n/a

 05-16-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) a écrit :
> Hi All,
>
> I want to know how many times 'Hi' will prints; and why?
>
>
> #include<stdio.h>
>
> int fact( int n );
>
> main() {
>
> int f;
>
> f = fact( 4 );
>
> printf("\nfact = %d", f);
> }
>
> int fact( int n ) {
> int fa;
>
> if ( n == 0 ) return 1;
>
> fa = n * fact( n - 1 );
>
> printf("\nHi");
>
> return fa;
> }
>

Or give us the address of your teacher. We will send him/her the
solution directly

Guest
Posts: n/a

 05-16-2006

(E-Mail Removed) wrote:
> I want to know how many times 'Hi' will prints; and why?

The first one is easy: just compile and run the code.
The second one is more tricky, as it involves exerting some actual

<snip textbook code>

venkatesh.k.desai5@gmail.com
Guest
Posts: n/a

 05-16-2006
Yeah I did it. Can you solve that trick?

Guest
Posts: n/a

 05-16-2006

(E-Mail Removed) wrote:
> Yeah I did it. Can you solve that trick?

Did what? What trick? Quote context. Read

In response to the above:

- Good for you.
- Yes I can.

Guest
Posts: n/a

 05-16-2006
(E-Mail Removed) wrote:
> Hi All,
>
> I want to know how many times 'Hi' will prints; and why?
>
>
> #include<stdio.h>
>
> int fact( int n );
>
> main() {
>
> int f;
>
> f = fact( 4 );
>
> printf("\nfact = %d", f);
> }
>
> int fact( int n ) {
> int fa;
>
> if ( n == 0 ) return 1;
>
> fa = n * fact( n - 1 );
>
> printf("\nHi");
>
> return fa;
> }
>

Better write it like this:

#include <stdio.h>

int fact(int n);

int main(void) {
int f;

f = fact(4);
printf("\nfact = %d\n", f);

return 0;
}

int fact(int n) {
int fa;

if (n==0) return 1;
fa = n*fact(n-1);
printf("\nHi");

return fa;
}

1) Declared main() as int main(void) and
2) added a '\n' to the last printf() to ensure that everything will be
printed.

You can now run the algorithm in piece of paper...

--
one's freedom stops where others' begin

Computer and Communications Engineering dept. (CCED)
University of Thessaly
http://dop.freegr.net/

Richard Heathfield
Guest
Posts: n/a

 05-16-2006
(E-Mail Removed) said:

> Hi All,
>
> I want to know how many times 'Hi' will prints; and why?

Too many, because you misimplemented your function to be recursive - a poor
way to calculate a factorial.

Also, what happens when you call fact(-4) instead of fact(4)?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)

CBFalconer
Guest
Posts: n/a

 05-16-2006
"(E-Mail Removed)" wrote:
>
> Yeah I did it. Can you solve that trick?

I'm glad you did. Did she like it? What does that have to do with
the C language?

In general on usenet you should realize that readers may very well
that they stand by themselves. Google is NOT usenet, it is only a
very poor interface to the real usenet system. To include proper
context when using google, see my sig. below. Please be sure to

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the

Clever Monkey
Guest
Posts: n/a

 05-16-2006
(E-Mail Removed) wrote:
> Yeah I did it. Can you solve that trick?
>

It's obvious that the magician simply moved the table to push the coin
through a hole. This, and slight of hand, made the coin *appear* to
pass into the glass and through the table.