{"id":1100,"date":"2014-12-08T00:01:05","date_gmt":"2014-12-08T08:01:05","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1100"},"modified":"2014-11-29T08:09:46","modified_gmt":"2014-11-29T16:09:46","slug":"making-change-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1100","title":{"rendered":"Making Change &#8211; Solution"},"content":{"rendered":"<p>To solve this month&#8217;s <a href=\" http:\/\/c-for-dummies.com\/blog\/?p=1083\">Exercise<\/a> you had to figure out a way to divvy up a dollar amount into the proper number of quarters, dimes, nickels, and pennies. As with many programming puzzles, a number of ways exist to code a solution. <a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/11\/12exercise.c\">Click here<\/a> to view mine.<br \/>\n<!--more--><br \/>\nIn my solution, I chose to whittle down the dollar amount by given chunks. It&#8217;s packing a suitcase by starting with the larger items first and then working down to the smaller ones.<\/p>\n<p>I started by creating variables to count each individual coin, plus another variable, <code>coins_input<\/code>, for the total:<\/p>\n<pre class=\"screen\">\r\n    int coins_input,quarters,dimes,nickels,pennies;\r\n\r\n    quarters = dimes = nickels = pennies = 0;\r\n    coins_input = 131;<\/pre>\n<p>Each coin variable is initialized to zero at Line 7. The amount of change I need to work with is set at Line 8.<\/p>\n<p>The bulk of the code is a <em>while<\/em> loop at Line 11. It keeps working as long as the value of <code>coins_input<\/code> is greater than zero. An <em>if-else<\/em> structure within the loop chips away individual coin values, largest to smallest:<\/p>\n<pre class=\"screen\">\r\n    while(coins_input)\r\n    {\r\n        if(coins_input &gt; 25)\r\n        {\r\n            quarters++;\r\n            coins_input -= 25;\r\n        }\r\n        else if (coins_input &gt; 10)\r\n        {\r\n            dimes++;\r\n            coins_input -= 10;\r\n        }\r\n        else if (coins_input &gt; 5)\r\n        {\r\n            nickels++;\r\n            coins_input -= 5;\r\n        }\r\n        else\r\n        {\r\n            pennies++;\r\n            coins_input--;\r\n        }\r\n    }<\/pre>\n<p>The <em>if-else<\/em> structure allows for only one action to happen at a time. Each decision, from the top down, deals with a smaller coin value. Quarters come first: If the total coin value is greater than 25, one quarter is removed, the value of <code>quarters<\/code> is incremented, the value of <code>coins_input<\/code> is reduced by 25, and then the loop repeats.<\/p>\n<p>When the value of <code>coins_input<\/code> is less than 25, the <em>else if<\/em> statement at Line 18 is executed. Those statements pop off dimes. When the value is less than 10, the <em>else if<\/em> at Line 23 is executed, popping off nickels. Finally come the pennies when the value of <code>coins_input<\/code> is less than 5.<\/p>\n<p>The <em>printf()<\/em> statement at Lines 34 through 38 displays the results:<\/p>\n<pre class=\"screen\">\r\n    printf(\"%2d quarters\\n%2d dimes\\n%2d nickels\\n%2d pennies\\n\",\r\n            quarters,\r\n            dimes,\r\n            nickels,\r\n            pennies);<\/pre>\n<p>The <code>%2d<\/code> placeholder keeps output to at least two characters wide for each integer. I split the line for each variable to make it more readable.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To solve this month&#8217;s Exercise you had to figure out a way to divvy up a dollar amount into the proper number of quarters, dimes, nickels, and pennies. As with many programming puzzles, a number of ways exist to code &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1100\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-1100","post","type-post","status-publish","format-standard","hentry","category-solution"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1100","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1100"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1100\/revisions"}],"predecessor-version":[{"id":1129,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1100\/revisions\/1129"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1100"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1100"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1100"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}