C++ Program to Generate All Unique Partitions of an Integer

«
»
This C++ Program demonstrates Generating All Unique Partitions of an Integer.

Here is source code of the C++ Program to demonstrate the Generation of All Unique Partitions of an Integer. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /* 
  2.  * C++ Program to Generate All Unique Partitions of an Integer 
  3.  */
  4. #include<iostream>
  5. using namespace std;
  6. /* 
  7.  * print an array p[] of size 'n'
  8.  */  
  9. void printArray(int p[], int n)
  10. {
  11.     for (int i = 0; i < n; i++)
  12.        cout << p[i] << " ";
  13.     cout << endl;
  14. }
  15.  
  16. void printAllUniqueParts(int n)
  17. {
  18.     int p[n];
  19.     int k = 0;
  20.     p[k] = n;
  21.     while (true)
  22.     {
  23.         printArray(p, k + 1);
  24.         int rem_val = 0;
  25.         while (k >= 0 && p[k] == 1)
  26.         {
  27.             rem_val += p[k];
  28.             k--;
  29.         }
  30.         if (k < 0)  
  31.             return;
  32.         p[k]--;
  33.         rem_val++;
  34.         while (rem_val > p[k])
  35.         {
  36.             p[k+1] = p[k];
  37.             rem_val = rem_val - p[k];
  38.             k++;
  39.         }
  40.         p[k+1] = rem_val;
  41.         k++;
  42.     }
  43. }
  44.  
  45. /* 
  46.  * Main
  47.  */
  48. int main()
  49. {
  50.     int value;
  51.     while(1)
  52.     {
  53.         cout<<"Enter an Integer(0 to exit): ";
  54.         cin>>value;
  55.         if (value == 0)
  56.            break;
  57.         cout << "All Unique Partitions of "<<value<<endl;
  58.         printAllUniqueParts(value);
  59.         cout<<endl;
  60.     }
  61.     return 0;
  62. }

advertisement
$ g++ unique_partitions.cpp
$ a.out
 
Enter an Integer(0 to exit): 2
All Unique Partitions of 2
2
1 1
 
Enter an Integer(0 to exit): 3
All Unique Partitions of 3
3
2 1
1 1 1
 
Enter an Integer(0 to exit): 4
All Unique Partitions of 4
4
3 1
2 2
2 1 1
1 1 1 1
 
Enter an Integer(0 to exit): 5
All Unique Partitions of 5
5
4 1
3 2
3 1 1
2 2 1
2 1 1 1
1 1 1 1 1
 
Enter an Integer(0 to exit): 7
All Unique Partitions of 7
7
6 1
5 2
5 1 1
4 3
4 2 1
4 1 1 1
3 3 1
3 2 2
3 2 1 1
3 1 1 1 1
2 2 2 1
2 2 1 1 1
2 1 1 1 1 1
1 1 1 1 1 1 1
 
Enter an Integer(0 to exit): 0
 
------------------
(program exited with code: 1)
Press return to continue

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn