Thread: Recursion stuck

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    9

    Question Recursion stuck

    How would I write a c program which will recursively compute the product of all even numbers greater than 0 and <= N, for values of N={16,7,9}, and print the results to an output file. For instance, if N=5, the product would be 4 x 2=8.

    This recursive function in my program should accept a single value for N and for every call to that recursive function print out 1. how many times the recursive function has been called so far, 2. the value of the argument to the recursive function for this particular call, and 3. the value of the product so far(before the next recursive call). Main() is not the recurisve function. An example output for N=9 for what I want it to do might be:

    Main: CALL ARGUMENT PRODUCT

    RECU: 1 8 8
    RECU: 2 6 48
    RECU: 3 4 192
    RECU: 4 2 384

    MAIN: The product for N=5 is 8.

    Here is what I got so far with the help of others but I still can't figure out how to printout how many times the function has been called, the value of the argument to the function for this call, and value of the product so far(BEFORE THE NEXT FUNCTION CALL). I can get it to just print ouf the product after the whole program is done, for instance N=9, the product of all it's even numbers is 8*6*4*2=384
    Notice for the example call it prints out call 1, argument 8, and product 8(product 8 because the first number it gets is 8 and so there is no other numbers yet to multiply it to for the total)
    Last I still can't also figure out how to get is to run through and find the product of all even numbers of each number 16,7,9. Maybe I need to use an array and I also need to clear the screen for each new number, like after 16, it clears it for the next value 7.
    code:







    #include <stdio.h>


    int product_of_evens(int num)
    {
    static int call;
    call++;

    if (num<=0)
    return 1;
    if((num%2)==0)
    {
    return num*(product_of_evens(num-1));
    }
    else
    {
    return product_of_evens(num-1);
    }

    }



    int main()
    {
    int num=9;
    printf("\nTotal: %d\n",product_of_evens(num));
    return 0;
    }

  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    OK, here's some code that will do the first bit (I'll leave the multiple calls for the different numbers up to you - you can use a loop) -

    Code:
     int recurse(int num) 
    { 
    	static int call;
    	static int total;
    	int r=0; 
    
    	if(num%2!=0) 
    	num--; 
    	
    	if(num>0) 
    	{
    		call++;
    		printf("Call: %d Arg: %d Running Total: %d\n",call,
    			num,call==1?total=num:total*=num);
    		r=num*recurse(num-2);
    	}
     
    	if(num==0)
    		return 1;
    	else
    		return r;
    
    }
    
    int main() 
    { 
    	int num=9; 
    	printf("\nTotal: %d\n",recurse(num)); 
    	return 0;
    }
    zen

  3. #3
    Unregistered
    Guest
    int product_of_evens(int i)
    {
    static int n=0,p=1;
    if (i<=0)
    return 1;
    if((i%2)==0)
    {
    printf("%d %d %d\n",++n,i,p*=i);
    return i*(product_of_evens(i-1));
    }

    else
    return (product_of_evens(i-1));

    }
    /* the above programme actually performs 'i' number of iterations ..try one of these for exact iterations*/




    int product_of_evens(int i)
    {
    static int n=0,p=1;
    if (i<2)
    return 1;
    if((i%2)==0)
    {
    printf("%d %d %d\n",++n,i,p*=i);
    return i*(product_of_evens(i-1));
    }

    else
    {
    printf("%d %d %d\n",++n,i,p*=--i);
    return i*(product_of_evens(i-1));
    }

    }

    /*------------------------------------------------------------*/

    int product_of_evens(int i)
    {
    static int n=0,p=1;
    if (i<=0)
    return 1;
    if((i%2)==0)
    {
    printf("%d %d %d\n",++n,i,p*=i);
    return i*(product_of_evens(i-2));
    }

    else
    {
    printf("%d %d %d\n",++n,i,p*=--i);
    return i*(product_of_evens(i-2));
    }

    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template Recursion Pickle
    By SevenThunders in forum C++ Programming
    Replies: 20
    Last Post: 02-05-2009, 09:45 PM
  2. a simple recursion question
    By tetra in forum C++ Programming
    Replies: 6
    Last Post: 10-27-2002, 10:56 AM
  3. stack and recursion help needed!
    By LouB in forum C++ Programming
    Replies: 3
    Last Post: 07-01-2002, 02:19 PM
  4. To Recur(sion) or to Iterate?That is the question
    By jasrajva in forum C Programming
    Replies: 4
    Last Post: 11-07-2001, 09:24 AM
  5. selection sorting using going-down and going-up recursion
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-02-2001, 02:29 PM