You can win three kinds of basketball points, 1 point, 2 points, and 3 points. Given a total score n, print out all the combination to compose n.

Examples:
For n = 1, the program should print following:
1

For n = 2, the program should print following:
1 1
2

For n = 3, the program should print following:
1 1 1
1 2
2 1
3

For n = 4, the program should print following:
1 1 1 1
1 1 2
1 2 1
1 3
2 1 1
2 2
3 1

and so on …

Algorithm:
At first position we can have three numbers 1 or 2 or 3.
First put 1 at first position and recursively call for n-1.
Then put 2 at first position and recursively call for n-2.
Then put 3 at first position and recursively call for n-3.
If n becomes 0 then we have formed a combination that compose n, so print the current combination.

[ad type=”banner”]

Below is a generalized implementation. In the below implementation, we can change MAX_POINT if there are higher points (more than 3) in the basketball game.

[pastacode lang=”c” manual=”%23define%20MAX_POINT%203%0A%23define%20ARR_SIZE%20100%0A%23include%3Cstdio.h%3E%0A%20%0A%2F*%20Utility%20function%20to%20print%20array%20arr%5B%5D%20*%2F%0Avoid%20printArray(int%20arr%5B%5D%2C%20int%20arr_size)%3B%0A%20%0A%2F*%20The%20function%20prints%20all%20combinations%20of%20numbers%201%2C%202%2C%20…MAX_POINT%0A%20%20%20that%20sum%20up%20to%20n.%0A%20%20%20i%20is%20used%20in%20recursion%20keep%20track%20of%20index%20in%20arr%5B%5D%20where%20next%0A%20%20%20element%20is%20to%20be%20added.%20Initital%20value%20of%20i%20must%20be%20passed%20as%200%20*%2F%0Avoid%20printCompositions(int%20n%2C%20int%20i)%0A%7B%0A%20%0A%20%20%2F*%20array%20must%20be%20static%20as%20we%20want%20to%20keep%20track%0A%20%20%20of%20values%20stored%20in%20arr%5B%5D%20using%20current%20calls%20of%0A%20%20%20printCompositions()%20in%20function%20call%20stack*%2F%0A%20%20static%20int%20arr%5BARR_SIZE%5D%3B%0A%20%0A%20%20if%20(n%20%3D%3D%200)%0A%20%20%7B%0A%20%20%20%20printArray(arr%2C%20i)%3B%0A%20%20%7D%0A%20%20else%20if(n%20%3E%200)%0A%20%20%7B%0A%20%20%20%20int%20k%3B%20%0A%20%20%20%20for%20(k%20%3D%201%3B%20k%20%3C%3D%20MAX_POINT%3B%20k%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20arr%5Bi%5D%3D%20k%3B%0A%20%20%20%20%20%20printCompositions(n-k%2C%20i%2B1)%3B%0A%20%20%20%20%7D%0A%20%20%7D%0A%7D%0A%20%0A%2F*%20UTILITY%20FUNCTIONS%20*%2F%0A%2F*%20Utility%20function%20to%20print%20array%20arr%5B%5D%20*%2F%0Avoid%20printArray(int%20arr%5B%5D%2C%20int%20arr_size)%0A%7B%0A%20%20int%20i%3B%0A%20%20for%20(i%20%3D%200%3B%20i%20%3C%20arr_size%3B%20i%2B%2B)%0A%20%20%20%20printf(%22%25d%20%22%2C%20arr%5Bi%5D)%3B%0A%20%20printf(%22%5Cn%22)%3B%0A%7D%0A%20%0A%2F*%20Driver%20function%20to%20test%20above%20functions%20*%2F%0Aint%20main()%0A%7B%0A%20%20int%20n%20%3D%205%3B%0A%20%20printf(%22Differnt%20compositions%20formed%20by%201%2C%202%20and%203%20of%20%25d%20are%5Cn%22%2C%20n)%3B%0A%20%20printCompositions(n%2C%200)%3B%0A%20%20getchar()%3B%0A%20%20return%200%3B%0A%7D” message=”C program” highlight=”” provider=”manual”/] [ad type=”banner”]