Thread: recursion - problem.

  1. #1
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    recursion - problem.

    I have a problem on my book, he wants me to build a program that will print all possible combinations (N-1) that the sum of them will be N.

    I have this code as far:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void recur(int n); //our recursive function
    
    int main(void) {
        int nNum;
        
        //get data, check it and act
        printf("Insert a number: ");
        if(!scanf("%d",&nNum)) {
            printf("Invalid input\n"); 
            exit(1);
        }
        
        //call ou recursive function        
        recur(nNum);
        
        //pause and exit
        system("PAUSE");
        return 0;
    }
    
    void recur(int n) {
        if(n == 0)
            return;
        else if(n == 1) {
            printf("1");
            return;
        }
        else if(n == 2) {
            printf("11");
            return;
        }
        
        else {
            printf("1");
            recur(n-1);
        }
    }
    I can't understand what to do next, don't make me the code, just try to explain me how to do it.

    Hmm, I cannot use any static variables or loops in the recursive function.

    Thanks.

  2. #2
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284

    Re: recursion - problem.

    Originally posted by Vber
    I have a problem on my book, he wants me to build a program that will print all possible combinations (N-1) that the sum of them will be N.
    I couldn't understand what your problem is. Do you want all collections of positive integers that add up to N?
    The one who says it cannot be done should never interrupt the one who is doing it.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Location
    Mexico City
    Posts
    33
    Hi.

    I think this will work:

    First read the number in N with scanf(). Then call the following function with these two parameters:

    recur ( 1, N-1)


    void recur(int N1 , int N2) {

    if (N1<=N2) {
    printf("%d %d", N1,N2);
    recur(N1 + 1, N2 - 1);




    Be sure that the number N is non zero, positive, and greater than 1


    Ps: close the brackets, i cant find them in this keyboard
    If you want to be happy one hour: take a nap
    if you want to be happy one day: go fishing
    If you want to be happy a year: inherit a fortune
    if you want to be happy for a life time: HELP SOMEBODY
    chinisse say.

  4. #4
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    I forgot to say, I can only pass one parameter.

  5. #5
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Are you allowed to pass structures? If so the restriction on the number of variables is rather meaningless.
    Code:
    #include <stdio.h>
    
    typedef struct info
    {
    	int i;
    	int j;
    } info;
    
    void show(info *in)
    {
    	if(in->i <= in->j)
    	{
    		printf("%d %d\n",in->i,in->j);
    		in->i++;in->j--;
    		show(in);
    	}
    }
    
    #define N 11
    int main(void)
    {
    	info n;
    	n.i=0;
    	n.j=N;
    	show(&n);
    	return 0;
    }
    The one who says it cannot be done should never interrupt the one who is doing it.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Use a void pointer as your function parameter, and pass it an array of intgers, then type cast it inside the function. That wouldn't technicly violate your single parameter issue either, and would get around the structure issue if you aren't allowed to use them.

    Or hell, just have your parameter be an array of integers.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    I think a watertight set of restrictions would be
    [list=1][*]dont pass pointers[*]don't pass structures[*]no global or static variables[*] single parameter[/list=1]
    or simply give a prototype
    like
    Code:
    int show(int N);
    The one who says it cannot be done should never interrupt the one who is doing it.

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. Recursion problem
    By trnd in forum C Programming
    Replies: 2
    Last Post: 02-01-2009, 03:06 PM
  3. Problem of understanding recursion
    By ixing in forum C Programming
    Replies: 2
    Last Post: 05-02-2004, 03:52 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. recursion problem
    By dustinc in forum C++ Programming
    Replies: 1
    Last Post: 10-29-2001, 04:29 AM