CS19002 Programming and Data Structures Laboratory, Section 5

Spring 2007

Assignment 4


Submission site  |  Show solution  |  Hide solution ]


This assignment is a continuation of Assignment 3. In this assignment you are asked to print all partitions of a positive integer. (In the earlier assignment you are asked to report only the count of partitions.) Store in an array all summands chosen so far. A recursive call is made after a new summand is chosen. Append this summand to the array and pass the appended array to the recursive call. When the recursion ends (i.e., the balance becomes zero), print the summand stored in the array one by one from beginning to end.

Part 1 [Credit: 70%]

Write a function that prints all partitions of a positive integer n.

Part 2 [Credit: 20%]

Write a function that prints all partitions of a positive integer n without repeating summands.

Part 3 [Credit: 10%]

Write a function that prints all partitions of a positive integer n having odd numbers of summands.

Note: Make a copy of your program for assignment 3 and modify the copy.

Report the output of your program on the following values of n.

    6
    8
   10
   12
   14

Sample output

   Enter n: 5
   ****** Part 1: All possible partitions
   Total count = 7
   1+1+1+1+1
   1+1+1+2
   1+1+3
   1+2+2
   1+4
   2+3
   5
   ****** Part 2: Partitions without repetitions
   Total count = 3
   1+4
   2+3
   5
   ****** Part 3: Partitions into odd number of parts
   Total count = 4
   1+1+1+1+1
   1+1+3
   1+2+2
   5


Submission site  |  Show solution  |  Hide solution ]


Back  |  Home