Thread: Please Help recursion

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

    Unhappy Please Help recursion

    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=5 for what I want it to do might be:

    Main: CALL ARGUMENT PRODUCT

    RECU: 1 4 4
    RECU: 2 2 8

    MAIN: The product for N=5 is 8.

    Here is what I got so far and had some help with other another programmer, but still stuck*NOTE(I know the program does not compute the right value, I need help on that too, plus I don't know what to do about passing the 3 values for N into the function)
    code:

    main()
    {
    int num=9;
    recurse(num);

    }

    int recurse(int num)
    {
    if(num%2!=0)
    num--;

    int r=0;
    if(num>0)
    r=num*(num-2)+recurse(num-2);

    printf("%d",r);
    return r;

    }

  2. #2
    Unregistered
    Guest
    easy:
    just change this
    int r=0; <<----- COMMENT THAT OUT, each time it calls the recurse function, it keeps resetting r to zero, thus you will never have the correct sum.
    The rest is fine
    if(num>0)
    r=num*(num-2)+recurse(num-2);

  3. #3
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    No, you'll need r initialised to zero, otherwise calculating r=n*(n-2) will produce random results. You could create a couple of static variables to store the running total and function call number -

    Code:
    #include <stdio.h>
    
    
    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,total+=num*(num-2));
    		r=num*(num-2)+recurse(num-2);
    	}
     
    	return r; 
    
    }
    
    int main() 
    { 
    	int num=9; 
    	printf("\nTotal: %d\n",recurse(num)); 
    	return 0;
    }
    zen

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2
    /*maybe buggy.!! but try this */

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

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2
    /*maybe buggy.!! but try this */

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

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. convert Recursion to linear can it be done
    By umen242 in forum C++ Programming
    Replies: 2
    Last Post: 10-15-2008, 02:58 AM
  3. Recursion... why?
    By swgh in forum C++ Programming
    Replies: 4
    Last Post: 06-09-2008, 09:37 AM
  4. a simple recursion question
    By tetra in forum C++ Programming
    Replies: 6
    Last Post: 10-27-2002, 10:56 AM
  5. 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