Thread: Integer partitioning

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    3

    Exclamation Integer partitioning

    can someone give me the c code for integer partitoning

    i.e. for the integer 4 u must print

    4
    3 1
    2 2
    2 1 1
    1 1 1 1

    I wrote the following code interpreting a java code, but it isn,t working, I want much simpler version of the following code.

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <string.h>
    
    void partition (int n, int max, char prefix[]);
    char * processAnswer (char prefix[], int n);
    
    int main ()
    {
        int n;
        int flag;
        
        while (1){
              printf ("Enter the number (integer): ");
              flag = scanf ("%d", &n);
              
              if (flag==0)
              {
                 printf ("Not an integer");
              }
              else if (flag == 1)
              { 
                 break;
              }
        }
        
        partition (n, n, "");
        
        int x;
        scanf ("%d", &x);
              
        return 0;
    }
    
    
    void partition (int n, int max, char prefix[])
    {
         if (n==0)
         {
            printf ("%s", prefix);
            return;
         }
         
         int min;
         int i;
         char * ptr;
         
         if (n<max)
         {
            min = n;
         }
         else
         {
             min = max;
         }
         
         for (i=0; i>=min; i+-1)
         {
            char * ch =  processAnswer (prefix, i);
            partition ( (n-i), i, ch );
         }
         
    }
    
    char * processAnswer (char prefix[], int n)
    {
         char ans[100];
         char ch; 
         int i=0;        
         
         ch=prefix[0];
         while (ch!='\0')
         {
               ans[i] = ch;
               i+=1;
               ch=prefix[i];
         }
         
         ans[i] = ' ';
         
         i+=1;
         ans[i] = (char) n;
         
         i+=1;
         ans[i] = '\0';
         
         char * ptr = &ans[0];
         return ptr; 
    }

    Thankz in advance

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > char * ptr = &ans[0];
    You're returning a pointer to a local variable - that is, it goes out of scope and no longer exists when the function returns.

    It's probably simpler to declare your array in partition(), and pass it as an 'out' parameter (where you want the answer stored), when you call processAnswer
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  4. No Match For Operator+ ???????
    By Paul22000 in forum C++ Programming
    Replies: 24
    Last Post: 05-14-2008, 10:53 AM
  5. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM

Tags for this Thread