{"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=&#8221;banner&#8221;]\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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C program<\/span> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">#define MAX_POINT 3<br\/>#define ARR_SIZE 100<br\/>#include&lt;stdio.h&gt;<br\/> <br\/>\/* Utility function to print array arr[] *\/<br\/>void printArray(int arr[], int arr_size);<br\/> <br\/>\/* The function prints all combinations of numbers 1, 2, ...MAX_POINT<br\/>   that sum up to n.<br\/>   i is used in recursion keep track of index in arr[] where next<br\/>   element is to be added. Initital value of i must be passed as 0 *\/<br\/>void printCompositions(int n, int i)<br\/>{<br\/> <br\/>  \/* array must be static as we want to keep track<br\/>   of values stored in arr[] using current calls of<br\/>   printCompositions() in function call stack*\/<br\/>  static int arr[ARR_SIZE];<br\/> <br\/>  if (n == 0)<br\/>  {<br\/>    printArray(arr, i);<br\/>  }<br\/>  else if(n &gt; 0)<br\/>  {<br\/>    int k; <br\/>    for (k = 1; k &lt;= MAX_POINT; k++)<br\/>    {<br\/>      arr[i]= k;<br\/>      printCompositions(n-k, i+1);<br\/>    }<br\/>  }<br\/>}<br\/> <br\/>\/* UTILITY FUNCTIONS *\/<br\/>\/* Utility function to print array arr[] *\/<br\/>void printArray(int arr[], int arr_size)<br\/>{<br\/>  int i;<br\/>  for (i = 0; i &lt; arr_size; i++)<br\/>    printf(&quot;%d &quot;, arr[i]);<br\/>  printf(&quot;\\n&quot;);<br\/>}<br\/> <br\/>\/* Driver function to test above functions *\/<br\/>int main()<br\/>{<br\/>  int n = 5;<br\/>  printf(&quot;Differnt compositions formed by 1, 2 and 3 of %d are\\n&quot;, n);<br\/>  printCompositions(n, 0);<br\/>  getchar();<br\/>  return 0;<br\/>}<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\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}]}}