{"id":1961,"date":"2016-06-08T00:01:01","date_gmt":"2016-06-08T07:01:01","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1961"},"modified":"2016-06-04T11:19:36","modified_gmt":"2016-06-04T18:19:36","slug":"find-the-best-size-container-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1961","title":{"rendered":"Find the Best Size Container &#8211; Solution"},"content":{"rendered":"<p>The C library contains various mathematical rounding functions, such as <em>ceil()<\/em>. That function, however, rounds up floating point values to the next integer. For <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1941\">this month&#8217;s Exercise<\/a>, your job was to round up an integer to multiples of 16: 16, 32, 48, and 64.<br \/>\n<!--more--><br \/>\nIn my solution, I had to figure out the difference between the value given and the next increment of 16. To do that, I relied upon my old mathematical pal, the modulus operator, <code>%<\/code>. The key operation is:<\/p>\n<pre><code>16 - (<em>value<\/em> % 16)<\/code><\/pre>\n<p>This statement obtains the difference between a <em>value<\/em> and the next increment of 16. So if the <em>value<\/em> is 22, you get  10 as a result, and <code>22 + 10 = 32<\/code>. Here&#8217;s how that works:<\/p>\n<pre><code>16 - (22 % 16)\r\n16 - (6)\r\n10<\/code><\/pre>\n<p>The 22 modulo 16 is 6, but <code>22 + 6 = 28<\/code>, which isn&#8217;t a multiple of 16. So you must subtract 6 from 16 to get 10. That way, when you add the result to the original value, you get: <code>22 + 10 = 32<\/code>. And 32 is the next-highest multiple of 16. The full statement is:<\/p>\n<pre><code><em>value<\/em> + (16 - <em>value<\/em> % 16)<\/code><\/pre>\n<p>An exception must be made for multiples of 16, as you can see in my sample solution here:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\n#define COUNT 5\r\n\r\nint main()\r\n{\r\n    int lengths[COUNT] = { 4, 28, 52, 16, 35 };\r\n    int x,container;\r\n\r\n    for(x=0; x&lt;COUNT; x++)\r\n    {\r\n        if(lengths[x]%16 == 0)\r\n            container = lengths[x];\r\n        else\r\n            container = lengths[x] + (16 - lengths[x]%16);\r\n        printf(\"Length of %d fits into %d\\n\",\r\n                lengths[x],\r\n                container);\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Line 12 tests to see whether the original value, stored in <code>lengths[x]<\/code>, is a multiple of 16. If so, the element is assigned to variable <code>container<\/code> and displayed in the output (Line 16). If not, the statement at Line 15 determines the value of variable <code>container<\/code>.<\/p>\n<p>Here&#8217;s the output:<\/p>\n<pre><code>Length of 4 fits into 16\r\nLength of 28 fits into 32\r\nLength of 52 fits into 64\r\nLength of 16 fits into 16\r\nLength of 35 fits into 48<\/code><\/pre>\n<p>One thing I had to keep pounding into my head for this solution was to remember that the modulus operator obtains the <em>remainder<\/em>, not the difference. The remainder between <em>value<\/em> and 16 is <code><em>value<\/em> % 16<\/code>. The difference is <code>16 - <em>value<\/em> % 16<\/code>. And you don&#8217;t need to use parentheses in that statement because the <code>%<\/code> operator has higher precedence than the <code>-<\/code> (subtraction) operator.<\/p>\n<p>As usual, your solution can be different from mine. I&#8217;d be curious to see whether anyone devised a solution that didn&#8217;t use the modulus operator.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The C library contains various mathematical rounding functions, such as ceil(). That function, however, rounds up floating point values to the next integer. For this month&#8217;s Exercise, your job was to round up an integer to multiples of 16: 16, &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1961\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-1961","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\/1961","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=1961"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1961\/revisions"}],"predecessor-version":[{"id":1973,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1961\/revisions\/1973"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1961"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1961"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1961"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}