Thread: Factrorial using conditonal operator in a macro?

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    57

    Factrorial using conditonal operator in a macro?

    Can I find the factorial of a number using conditional operator in a macro definition?

    #define fact(n) ( n == 0 ? 1 ; (n*(fact(n-1) ) ) )

    since recursive definition is not allowed in macros, is there any work around?

    Thanks,
    Anoop.

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    You can solve the problem by removing the recursion and using a while-loop or such instead.

    Shiro

  3. #3
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    As far as I now, the ?: operator is C++ not C. Correct me if wrong.
    Second, it is not:
    Code:
    #define fact(n) ( n == 0 ? 1 ; (n*(fact(n-1) ) ) )
    But:
    Code:
    #define fact(n) ( n == 0 ? 1 :(n*(fact(n-1) ) ) )
    Nothing more to tell about me...
    Happy day =)

  4. #4
    Registered User
    Join Date
    Dec 2003
    Posts
    53
    first off, ? exists in C, second off, changing that still doesn't make that macro make sense...

    Essentially, macros are a shortcut for replacing certain types of things in the code when you compile it. for example, if I type this into my code:
    #define MAX 30
    when I compile my code, every instance of MAX will be replaced by 30...
    I think if he defines n to a number before that recursive macro, it MIGHT be compilable, but his program's size may increase exponentally relative the the value of n....

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    57
    Okie that ";" obviously was a typo. Sorry for that.

    As tzuchan has pointed out, it exists in C.

    Now, defining N might not work since the compiler would look for
    #define TOKEN VALUE
    and would complain if the VALUE is expressed in terms of TOKEN again. Moreover, if you define N, you dont need to put a conditional operator.

    What I wanted to know was if someone has cheated the compiler and calculated the factorial using the conditional operator in a macro.

    Generalizing, has anyone, used recursive MACRO?

    Thanks,
    Anoop.

  6. #6
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >Generalizing, has anyone, used recursive MACRO?
    You can't. It simply replaces the text. Take this code for example:
    Code:
    #define for if( 0 ); else for
    
    int main( void )
    {
    
      int i;
      for( i = 0; i < 5; ++i )
        putc( '0' + i );
    
      return 0;
    
    }
    This is what it might look like after the macros are dealt with:
    Code:
    int main( void )
    {
    
      int i;
      if( 0 ); else for( i = 0; i < 5; ++i )
        putc( '0' + i );
    
      return 0;
    
    }
    All it does is replaces every instance of 'for' in the code with 'if( 0 ); else for'. No recursion. Can't do it.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  7. #7
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    You can use a macro for calculating the factorial of n like this:

    Code:
    #define FAC(n) \
    for (f = 1, i = 2; i <= n; i++) \
    { \
      f *= i; \
    }
    Note that if you use such a macro, then in the function where you use it, the variables, n, f and i must have been declared before the macro can be used. A macro can't be recursive, since a macro isn't a call to something. As XSquared said, it is a replacement of some text.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  3. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  4. L macro
    By George2 in forum C Programming
    Replies: 1
    Last Post: 08-20-2007, 09:24 AM
  5. need help with multi line macro usage
    By Cdigitproc1 in forum C Programming
    Replies: 9
    Last Post: 04-29-2005, 09:50 AM