Thread: Integer partition

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    18

    Integer partition

    Q.A partition of a positive integer n is a sequence of positive integers that sum to n. Write a program to print all non-increasing partitions of n.

    eg. n=4
    4
    3 1
    2 2
    2 1 1
    1 1 1 1

    Code:
    #include<stdio.h>
    
    
    void printint(int, int);
    int main(void)
    {
    	int num;
    	printf("Enter number: ");
    	scanf("%d", &num);
    	
    	printint(num, 0);
    	
    	return 0;
    }
    
    
    void printint(int n, int a)
    {
    	int j ,k;
    	j = n-a;
    	k = n-j;
    	
    	printf("%d%d\n", j, k);
    	a++;
    	
    	if(k>1)
    	{
    		printf("%d", j);
    		printint(k, k-1);
    	}	
    	if(j!=1) 
    	printint(n,a);
    	
    
    
    }
    My code doesnt work for number more than 3! Any idea? Is there any other way to solve this problem? In need of help! This is not a homework question! Thanks!

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Of course it's homework. In more than 20 years of coding, I've never yet seen a situation where I would need to do this.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    18
    Its for my own self practice that I found it from the Internet Recursion!HAHA

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's still homework. What good does it do me to give you an answer for your "own self practice"? Also, anything you can do with recursion, you can do without recursion.


    Quzah.
    Last edited by quzah; 10-28-2011 at 05:25 AM.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Partition and scheduling HW/SW help!
    By thangdc01_02 in forum C++ Programming
    Replies: 3
    Last Post: 11-18-2010, 02:07 PM
  2. Integer partition
    By muhFah in forum C Programming
    Replies: 3
    Last Post: 09-18-2009, 10:44 AM
  3. memory partition
    By sarathius in forum C Programming
    Replies: 8
    Last Post: 03-03-2008, 04:05 AM
  4. an invisible partition?
    By Queatrix in forum Tech Board
    Replies: 11
    Last Post: 12-11-2006, 04:10 PM
  5. Partition Question....
    By (TNT) in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 11-27-2001, 11:59 AM