counting and printing partitions of 1 to n-1

Pages: 12
I am trying write a recursive function(it must be recursive) to print out the partitions and number of partitions for 1 to n-1.
For example, 4 combinations that sum to 4:
1 1 1 1
1 1 2
1 3
2 2

I am just having much trouble with the function. This function below doesn't work. Can someone help me please?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 int partition(int n, int max)
{
  
  if(n==1||max==1)
    return(1);
  int counter = 0;
  if(n<=max)
    counter=1;
  for(int i = 0; n>i; i++){
          n=n-1;
          cout << n << "+"<< i <<"\n";
          counter++;
          partition(n,i);         
        }
 
  return(counter);
}
Thanks, like I said in my other post, for now I have to do things manually. You think you can help?
Oh, sorry I didn't see that you had to write it yourself.

What specifically isn't working about it? Does it crash? Does it step on your cat? Does it send a virus to your mayor? You need to be specific.
well I if I set n=4 I want it to print
1 1 1 1
1 1 2
1 3
2 2

I just cannot figure out how to get there
Why does your function have a return value?
so in the main the I can cout the amount of partitions. I know it could be void
That value can be calculated with a simple expression from the parameters you pass in.
what would those parameters be?
You're the one calling the function, you should already have the parameters...otherwise you wouldn't even be able to call the function...
Thanks, I have a question about that function

1
2
3
4
5
6
7
8
9
10
void printPartitions(int target, int maxValue, String suffix) {
    if (target == 0)
        System.out.println(suffix);
    else {
        if (maxValue > 1)
            printPartitions(target, maxValue-1, suffix);
        if (maxValue <= target)
            printPartitions(target-maxValue, maxValue, maxValue + " " + suffix);
    }
}

what does System.out.println(suffix); mean?
The code is written in Java, not C++ - the statement you're confused about is how output is done in Java.
so how would I use that in c++?
You have to rewrite the equivalent code in C++. I know you know how to output things in C++.
I do not see what is wrong with this code, I just need to be able to cout the answer

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void printPartitions(int target, int maxValue,  string &suffix)
{
	if (target == 0)
	{
		cout << suffix << endl;
	}
	else
	{
		if (maxValue > 1)
		{
			printPartitions(target, maxValue-1, suffix);
		}
		if (maxValue <= target)
		{
			printPartitions(target - maxValue, maxValue, maxValue + " " + suffix);
		}
	}
}
Java allows concatenating numbers and strings, whereas C++ does not. You need to convert the numbers to strings first:
http://www.cplusplus.com/reference/string/to_string/
http://en.cppreference.com/w/cpp/string/basic_string/to_string
but where in the function would convert the numbers to strings? in the recursion call?
also any advice on my other question?
al547 wrote:
but where in the function would convert the numbers to strings? in the recursion call?
Line 15, the last parameter? It should not be difficult to spot.
al547 wrote:
also any advice on my other question?
What other question?
Pages: 12