I'm writing a C program that will print the following sequence As an Example If n=5
5
4 1
3 2
3 1 1
2 2 1
2 1 1 1
1 1 1 1 1
i have write a code and one of my friend make some changes to it and now it working properly. But i couldn't understand what he has done. the code is written here below
#include <stdio.h>
int Min(int Number1, int Number2)
{
if(Number1 <= Number2)
return Number1;
else
return Number2;
}
void Partition(int Number, int Lim, const char* Str)
{
int Count = 0;
char Out[50];
if (Number > 0)
{
for(Count = Min(Number, Lim);Count > 0;Count-- )
{
sprintf(Out, "%s %d", Str, Count);
Partition(Number - Count, Count, Out);
}
}
else
printf("%s\n", Str);
}
int main()
{
int Number = 0;
char Str[50];
printf("Enter a number: ");
scanf("%d", &Number);
if (Number < 1)
{
printf("Number is Negative.\n");
return -1;
}
sprintf(Str, "%d = ", Number);
Partition(Number, Number, Str);
return 0;
}
if you need any psedocode help here it is: but i'm not sure wheather this is correct or not. Since i'm beginner for c plz help.
Procedure Min (Number1, Number2)
1.If Number1<=Number2
2. then return Number1
3. else return Number2
Procedure Partition (Number, Lim, Str)
1. count 0
2. if Number > 0
3. then for count Min (Number, Lim) down to 1
4. print (Str, count)
5. Partition(Number-count,count,Out)
6. else
7. then print(Str)
Can't we do this using <math.h> in order to skip that "Min Function"