{"id":25713,"date":"2017-10-25T20:07:44","date_gmt":"2017-10-25T14:37:44","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25713"},"modified":"2017-10-25T20:07:44","modified_gmt":"2017-10-25T14:37:44","slug":"c-programming-print-combinations-points-can-compose-given-number","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-programming-print-combinations-points-can-compose-given-number\/","title":{"rendered":"C Programming-Print all combinations of points that can compose a given number"},"content":{"rendered":"<p>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.<\/p>\n<p>Examples:<br \/>\nFor n = 1, the program should print following:<br \/>\n1<\/p>\n<p>For n = 2, the program should print following:<br \/>\n1 1<br \/>\n2<\/p>\n<p>For n = 3, the program should print following:<br \/>\n1 1 1<br \/>\n1 2<br \/>\n2 1<br \/>\n3<\/p>\n<p>For n = 4, the program should print following:<br \/>\n1 1 1 1<br \/>\n1 1 2<br \/>\n1 2 1<br \/>\n1 3<br \/>\n2 1 1<br \/>\n2 2<br \/>\n3 1<\/p>\n<p>and so on \u2026<\/p>\n<p>Algorithm:<br \/>\nAt first position we can have three numbers 1 or 2 or 3.<br \/>\nFirst put 1 at first position and recursively call for n-1.<br \/>\nThen put 2 at first position and recursively call for n-2.<br \/>\nThen put 3 at first position and recursively call for n-3.<br \/>\nIf n becomes 0 then we have formed a combination that compose n, so print the current combination.<\/p>\n[ad type=\u201dbanner\u201d]\n<p>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.<\/p>\n[pastacode lang=\u201dc\u201d manual=\u201d%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\u2026MAX_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\u201d message=\u201dC program\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n","protected":false},"excerpt":{"rendered":"<p>C Programming-Print all combinations of points that can compose &#8211; Mathematical Algorithms -You can win three kinds of basketball points, 1 point, 2 points.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,69866,1,74058],"tags":[74843,72246,70860,69930,70812,70842,70815,74845,74848,74851,74833,69955,74847,72269,73721,70363,74830,74836,74826,70822,74841,74839,69959,70838,74066,74837,74842,74835,70828,74832,74069,74828,74829,74825,70331,70384,70347,74827,74849,74840,74846,74834,70810,70836,74844,74850,74831,74824,69952,74838],"class_list":["post-25713","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-c-programming","category-coding","category-mathematical-algorithms","tag-arithmetic-operators-in-c","tag-armstrong-number-in-c","tag-array-definition-in-c","tag-array-in-c-language","tag-array-in-c-programming","tag-array-in-c-programming-examples","tag-array-programs-in-c","tag-basic-c-program-examples","tag-basic-c-programming-tutorial","tag-basic-programs-in-c","tag-c-language-keywords","tag-c-language-operator","tag-c-progra","tag-c-program-for-armstrong-number","tag-c-program-for-permutation-of-numbers","tag-c-program-to-find-factorial","tag-c-programming-basics-notes","tag-c-programming-language-software","tag-c-programming-switch","tag-c-programs-with-output","tag-coding-languages","tag-computer-languages-list","tag-define-c-language","tag-example-of-c-program","tag-for-loop-c-programming","tag-function-c-programming","tag-function-in-c-language","tag-function-in-c-programming-examples","tag-functions-in-c-programming","tag-functions-in-c-programming-with-examples","tag-if-c-programming","tag-if-else-c-programming","tag-list-of-computer-languages","tag-most-used-programming-languages","tag-permutation-and-combination-formula","tag-permutation-program-in-c","tag-permutations-and-combinations-formula","tag-popular-programming-languages","tag-programming-language","tag-programming-language-popularity","tag-programming-languages","tag-programming-languages-list","tag-simple-c-programs","tag-simple-c-programs-with-output","tag-software-for-c-programming","tag-software-languages","tag-switch-case-in-c-programming-examples","tag-top-ten-programming-languages","tag-what-is-c-language-definition","tag-while-loop-in-c-programming-example"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25713","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=25713"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25713\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25713"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25713"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25713"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}