Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > int fact( int n )

Reply
Thread Tools

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;
}

 
Reply With Quote
 
 
 
 
jacob navia
Guest
Posts: n/a
 
      05-16-2006
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;
> }
>


Do your own homework.

Or give us the address of your teacher. We will send him/her the
solution directly
 
Reply With Quote
 
 
 
 
Vladimir Oka
Guest
Posts: n/a
 
      05-16-2006

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
effort in learning about C.

<snip textbook code>

 
Reply With Quote
 
venkatesh.k.desai5@gmail.com
Guest
Posts: n/a
 
      05-16-2006
Yeah I did it. Can you solve that trick?

 
Reply With Quote
 
Vladimir Oka
Guest
Posts: n/a
 
      05-16-2006

wrote:
> Yeah I did it. Can you solve that trick?


Did what? What trick? Quote context. Read
<http://cfaj.freeshell.org/google/>.

In response to the above:

- Good for you.
- Yes I can.

See also: Jacob Navia's post.

 
Reply With Quote
 
Giannis Papadopoulos
Guest
Posts: n/a
 
      05-16-2006
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

Giannis Papadopoulos
Computer and Communications Engineering dept. (CCED)
University of Thessaly
http://dop.freegr.net/
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      05-16-2006
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)
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      05-16-2006
"" 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
not have convenient access to previous articles in a thread. That
means that your reply articles should include adequate context, so
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
read the referenced URLs.

--
"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
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>


 
Reply With Quote
 
Clever Monkey
Guest
Posts: n/a
 
      05-16-2006
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why is int a[0] not allowed, but int* a = new int[0] is? haijin.biz@gmail.com C++ 9 04-17-2007 09:01 AM
Difference between int i, j; and int i; int j; arun C Programming 8 07-31-2006 05:11 AM
int a[10]; int* p=(int*)((&a)+1); But why p isn't equal to ((&a)+1)? aling C++ 8 10-20-2005 02:42 PM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM
dirty stuff: f(int,int) cast to f(struct{int,int}) Schnoffos C Programming 2 06-27-2003 03:13 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57