Recursive factorial, what is wrong with my code?

This is a discussion on Recursive factorial, what is wrong with my code? within the C Programming forums, part of the General Programming Boards category; got access violation when running in VC++ 6 Code: #include<stdio.h> void p(int n){ if(n > 0){ p(n - 2); printf("%d ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    12

    Question Recursive factorial, what is wrong with my code?

    got access violation when running in VC++ 6

    Code:
    #include<stdio.h>
    
    
    void p(int n){
       
       if(n > 0){
         p(n - 2); 
         printf("%d ", n); 
         p(n - 1);
        }
    } 
    int main(void) {
       p(4);
       
    return 0;   
    }

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I don't see anything in the code that would cause you to go too deep on a recursion to get an access violation.

  3. #3
    Compulsive Liar Robc's Avatar
    Join Date
    Jul 2004
    Posts
    149
    Just out of curiosity, how does that function calculate a factorial recursively? It really should look a bit more like this:
    Code:
    int f(int n)
    {
      if (n < 2) {
        return 1;
      }
    
      return n * f(n - 1);
    }

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by xephyr
    got access violation when running in VC++ 6

    Code:
    #include<stdio.h>
    
    
    void p(int n){
       
       if(n > 0){
         p(n - 2); 
         printf("%d ", n); 
         p(n - 1);
        }
    } 
    int main(void) {
       p(4);
       
    return 0;   
    }
    You aren't using recursion there dude, take a look here:
    Recursive function confusion
    That's a good example of recursion explained step by step.

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Um any function that calls itself IS recursion. In fact there are two recursion points in that function.

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by Thantos
    Um any function that calls itself IS recursion. In fact there are two recursion points in that function.
    Can you please me point them cause I see just one?

  7. #7
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Code:
    p(n-2)
    ...
    p(n-1)

  8. #8
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by sean_mackrory
    Code:
    p(n-2)
    ...
    p(n-1)
    Wow I should be drunk when I posted before, sorry dude now I got it

  9. #9
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    it's strange that people first learn recursion by writing the factorial function, it's simple, but terribly inefficient and really a poor use of recursion and it's capabilities. Recursion is better used in text analysis, sorting and data structure manipulation.

  10. #10
    Compulsive Liar Robc's Avatar
    Join Date
    Jul 2004
    Posts
    149
    >it's strange that people first learn recursion by writing the factorial function
    Not strange at all really. As you said, it's simple and easy to trace. That makes it a good candidate for a first step into the concept of recursion. The same goes with the fibonacci recursive solution. Neither of them should be used in any real code, but they're a critical step for beginners.

  11. #11
    Registered User
    Join Date
    Jul 2004
    Posts
    12

    Wrong code,sorry, Look at this

    Code:
    #include <stdio.h>
    int factorial(int n);
    int main()
    {
      
      factorial(2);
      for(;;);
      return 0;
    }
    int factorial (int n)
    {
    	int answer;
    		if (n==1) return 1;
    		answer=factorial(n-1)*n;
    		printf("%d\n", answer);
    		
    		return answer;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  2. What's wrong with this code?
    By Luciferek in forum C++ Programming
    Replies: 4
    Last Post: 06-21-2008, 12:02 PM
  3. What is wrong with my code? My first program......
    By coreyt1111 in forum C++ Programming
    Replies: 11
    Last Post: 11-14-2006, 01:03 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  5. recursive factorial function
    By brianptodd in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2003, 12:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21