C Program to Generate All the Set Partitions of n Numbers Beginning from 1 and so on

This C program generates all the set of partitions of n Numbers beginning from 1 to n.

This algorithm partitions an integer into numbers which sum up to form the original number. It generates partitions of a set of numbers for a given range.

Here is the source code of the C program to perform integer partition. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef struct {
  4. int first;
  5.      int n;
  6.      int level;
  7. } Call;
  8.  
  9.  
  10. void print(int n, int * a) {
  11.      int i ;
  12.      for (i = 0; i <= n; i++) {
  13.           printf("%d", a[i]);
  14.      }
  15.      printf("\n");
  16. }
  17.  
  18.  
  19. void integerPartition(int n, int * a){
  20.      int first;
  21.      int i;
  22.      int top = 0;
  23.      int level = 0;
  24.      Call * stack = (Call * ) malloc (sizeof(Call) * 1000);
  25.      stack[0].first = -1;
  26.      stack[0].n = n;
  27.      stack[0].level = level;
  28.      while (top >= 0){
  29.           first = stack[top].first;
  30.           n = stack[top].n;
  31.           level = stack[top].level;
  32.           if (n >= 1) {
  33.                if (first == - 1) {
  34.                     a[level] = n;
  35.                     print(level, a);
  36.                     first = (level == 0) ? 1 : a[level-1];
  37.                     i = first;
  38.                } else {
  39.                     i = first;
  40.                     i++;
  41.                }
  42.                if (i <= n / 2) {
  43.                     a[level] = i;
  44.                     stack[top].first = i;
  45.                     top++;
  46.                     stack[top].first = -1;
  47.                     stack[top].n = n - i;
  48.                     stack[top].level = level + 1;
  49.           } else {
  50.                top--;
  51.           }
  52.      } else {
  53.      top --;
  54.      }
  55. }
  56. }
  57.  
  58. int main(){
  59.     int N = 1;
  60.     int * a = (int * ) malloc(sizeof(int) * N);
  61.     int i;
  62.     printf("\nEnter a number N to generate all set partition from 1 to N: ");
  63.     scanf("%d", &N);
  64.     for ( i = 1; i <= N; i++)
  65.     {
  66.         printf("\nInteger partition for %d is: \n", i);
  67.         integerPartition (i, a);
  68.     }
  69.     return(0);
  70. }

$ gcc integer_partition.c -o integer_partition
$ ./integer_partition
Enter a number N to generate all set partition from 1 to N: 5
Integer partition for 1 is: 
1
 
Integer partition for 2 is: 
2
11
 
Integer partition for 3 is: 
3
12
111
 
Integer partition for 4 is: 
4
13
112
1111
22
 
Integer partition for 5 is: 
5
14
113
1112
11111
122
23

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.