{"id":1241,"date":"2015-03-08T00:01:07","date_gmt":"2015-03-08T08:01:07","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1241"},"modified":"2015-03-08T08:29:11","modified_gmt":"2015-03-08T15:29:11","slug":"pattern-manipulation-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1241","title":{"rendered":"Pattern Manipulation &#8211; Solution"},"content":{"rendered":"<p>This month&#8217;s <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1229\">Exercise<\/a> was to create a cycle of numbers, values that repeat within a certain range. Of all the Exercises presented so far on this site, this one probably has the greatest number of solutions.<br \/>\n<!--more--><br \/>\nOne of my first solutions used an array, which more inline with how animated graphics works: Each element in the array represents a color value for a specific panel in the display. Increment each of the panel&#8217;s values, then show the graphic. Repeat.<\/p>\n<p>The key to making the entire process work is the modulus operator, <code>%<\/code>. You need to cap the incrementing values so that they don&#8217;t overflow. In this case, eight values are being presented, 0 through 7, so the function is <code>x % 8<\/code>.<\/p>\n<p>It doesn&#8217;t matter how large an <em>int<\/em> value gets, the function <code>x % 8<\/code> always caps the result to the range of 0 through 7. Even if a <em>signed int<\/em> value cycles around from 2147483647 to -2147483648 and back to zero, the result of <code>x % 8<\/code> is always in the range 0 to 7.<\/p>\n<p>My first solution below uses a <em>while<\/em> loop to cycle the values. The <code>while(1)<\/code> at Line 8 sets an infinite loop.<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int base,x;\r\n\r\n    base = 0;\r\n    while(1)\r\n    {\r\n        for(x=0;x&lt;8;x++)\r\n        {\r\n            printf(\" %d\",(base+x)%8);\r\n        }\r\n        putchar('\\n');\r\n        base++;\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The value of <code>base<\/code> is incremented over and over (Line 15). Variable <code>x<\/code> provides an offset from that base in Line 12, which is how the values 0 through 7 are generated. That creates columnar data &mdash; a two-dimensional array &mdash; which could be used to cycle graphics.<\/p>\n<p>Because I mentioned it in my challenge, the following code does the same thing, but uses only one variable:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int base = 0;\r\n\r\n    while(1)\r\n    {\r\n        printf(\"%d %d %d %d %d %d %d %d\\n\",\r\n            base%8,\r\n            (base+1)%8,\r\n            (base+2)%8,\r\n            (base+3)%8,\r\n            (base+4)%8,\r\n            (base+5)%8,\r\n            (base+6)%8,\r\n            (base+7)%8);\r\n        base++;\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>While the above solution is possible, and it&#8217;s neatly formatted, it&#8217;s not very elegant programming. Any time I see a series of values +1, +2, +3, and so on, I question why the programmer didn&#8217;t use a loop. In this case, it&#8217;s simply to prove that a one-variable solution is possible.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This month&#8217;s Exercise was to create a cycle of numbers, values that repeat within a certain range. Of all the Exercises presented so far on this site, this one probably has the greatest number of solutions.<\/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-1241","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\/1241","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=1241"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1241\/revisions"}],"predecessor-version":[{"id":1260,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1241\/revisions\/1260"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1241"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1241"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1241"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}