{"id":373,"date":"2013-11-02T00:01:50","date_gmt":"2013-11-02T08:01:50","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=373"},"modified":"2013-10-26T07:29:04","modified_gmt":"2013-10-26T15:29:04","slug":"every-other-one","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=373","title":{"rendered":"Every Other One"},"content":{"rendered":"<p>You should be familiar with the C language modulus operator, <code>%<\/code>. Even if that familiarity is fear or confusion it&#8217;s still familiarity.<br \/>\n<!--more--><br \/>\nModulus is simply a diabolical-sounding term for &#8220;find the leftovers.&#8221; You first stumbled upon the modulus concept when learning long division: The modulus is related to the remainder, the leftover when one number doesn&#8217;t <em>gazinta<\/em> another equally. That concept is expressed in C programming by using the <code>%<\/code> (percent sign), the modulus operator.<\/p>\n<p>The modulus operator is commonly used between two integer values or variables. The result is the leftover of the first integer divided by the second. But why bother resolving that issue in your head when you can stare at some code?<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;time.h&gt;\r\n\r\nint main()\r\n{\r\n    int r,x;\r\n\r\n    srand((unsigned)time(NULL));    \/* seed the randomizer *\/\r\n    for(x=0;&lt;<20;x++)\r\n    {\r\n        r = rand() % 10 + 1;        \/* from 1 through 10 *\/\r\n        printf(\"%2d\\n\",r);\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Line 12 fetches a random value by using the <em>rand()<\/em> function. That value can be quite huge, depending on the C library's implementation of <em>rand()<\/em>. To pare it down to the range of 1 through 10 the modulus operator is used:<\/p>\n<p><code>rand() % 10<\/code><\/p>\n<p>This equation takes whatever integer value <em>rand()<\/em> generates and divides it by 10. The remainder is in the range of 0 through 9. Line 12 in the code adds one to the result, which makes the range 1 through 10. The program's output proves that:<\/p>\n<pre><code> 5\r\n 1\r\n 4\r\n 2\r\n10\r\n 2\r\n 6\r\n 7\r\n 5\r\n 4\r\n 6\r\n 9\r\n 1\r\n 2\r\n 3\r\n 1\r\n 2\r\n 5\r\n 9\r\n 3<\/code><\/pre>\n<p>It would be enough to leave the modulus operator alone at this point. The <code>%<\/code> makes a great slicer for the <em>rand()<\/em> function's return value, but that's not the limit of its abilities.<\/p>\n<p>For example, consider this output, which is created using the same code shown above but with a few modulus-related modifications:<\/p>\n<pre><code>  6\r\n* 3\r\n  3\r\n*10\r\n  1\r\n* 2\r\n 10\r\n* 2\r\n  5\r\n* 1\r\n  4\r\n* 9\r\n  3\r\n* 7\r\n  7\r\n* 2\r\n  3\r\n* 6\r\n  7\r\n* 8<\/code><\/pre>\n<p>See how every other line has an asterisk prefixed? Without peeking ahead, can you code such a thing? It's possible without using the modulus operator, but it's far easier when you use it.<\/p>\n<p>Remember that the modulus operator gives you the remainder. So if you want to pluck out every <em>n<\/em>th item in a list, you use the formula <code>% <em>n<\/em><\/code>. Here is the updated for loop from the preceding code example, the code that generates the every-other-line-asterisk output:<\/p>\n<pre class=\"screen\">\r\n    for(x=0;x&lt;20;x++)\r\n    {\r\n        r = rand() % 10 + 1;        \/* from 1 through 10 *\/\r\n        if(x % 2)\r\n            printf(\"*%2d\\n\",r);\r\n        else\r\n            printf(\" %2d\\n\",r);\r\n    }<\/pre>\n<p>The <em>if<\/em> comparison is TRUE when the value of <code>x<\/code> is odd (not evenly divisible by two). So for every odd \"row\" in the output, an asterisk prefixes the value. The even output (the <em>else<\/em> condition above) puts a space in the <em>printf()<\/em> function, which keeps each value lined up.<\/p>\n<p><a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2013\/11\/1102.c\">Click here<\/a> to see the full code.<\/p>\n<p>Now what if you wanted to place an asterisk by every third item? Can you modify the code to do that? It might not be as easy as you think, but you'll figure it out.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One good way to understand the modulus operator is to put it to practical use. For example, when paring down random numbers or as a way to skip every <em>n<\/em>th item in a list. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=373\">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":[2],"tags":[],"class_list":["post-373","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/373","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=373"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/373\/revisions"}],"predecessor-version":[{"id":387,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/373\/revisions\/387"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=373"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=373"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=373"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}