Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
We are going over recursion in my class and I do not quite understand it, I was wondering if someone can help me with this problem

let c(n) be the number of different group integers that can be chosen from the integers 1 through n-1, so that the integers in each group add up to n (for example, n=4=[1+1+1+1]=[1+1+2]=[2+2]). Write a recursive definition for c(n) under the following variations:
a) You count permutations. For example, 1,2,1 and 1,1,2 are two groups that each add up to 4
b)you ignore permutations

This is what I have but I cannot figure out how to fix it:

C++
int recurse_perm(int n)
    {
    	if (n == 1){
    		return 1;
    	}
    	int sum = 1;
        for(int i=n; i>1; i--){
                sum+=1;
                recurse_perm(n+1)+recurse_perm(n);
        }
    	return sum;
    }
Posted
Comments
Richard MacCutchan 3-Mar-15 4:00am    
I have deleted your duplicate of this question. Please do notrepost.

1 solution

Why do you think this code should work?
(You wrote it to do this assignment, I presume you thought it would work.)

For the moment, forget the code.
When I was TA of a programming class at Caltech, one of the first things I presented was:
If you can't write what you are doing in English (or your native language) then you can't write it in code.
So work through a couple/few of examples of this problem (both of the required versions).
Write down your assumptions!
Write down every step you are taking.
When you have a procedure that will work with different inputs,
then (and only then) start coding.
The code should reflect the procedure you have written.
Do not worry about efficiency yet.
You're looking for correct behavior.
Efficiently doing the wrong thing is still wrong.
And it would probably be harder to understand why it is wrong.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900