Click here to Skip to main content
15,902,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C
#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"
Posted
Updated 14-Sep-10 23:39pm
v5

1 solution

Well, try it and see what happens...
 
Share this answer
 
Comments
Strider1987 15-Sep-10 5:35am    
Problem is i've done that question by using two for loops and it doesn't work and here this code is not understandable for me

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