{"id":2948,"date":"2018-02-08T00:01:30","date_gmt":"2018-02-08T08:01:30","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2948"},"modified":"2018-02-03T10:24:26","modified_gmt":"2018-02-03T18:24:26","slug":"oscillation-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2948","title":{"rendered":"Oscillation &#8211; Solution"},"content":{"rendered":"<p>For my solution to this month&#8217;s <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2941\">Oscillation Exercise<\/a> I created a toggle variable. This variable switches between 1 and -1. It sets the direction for the cascading values.<br \/>\n<!--more--><br \/>\nHere is my solution:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n  \r\nint main()\r\n{\r\n    int count,value,direction;\r\n\r\n    value = count = 0;\r\n    direction = 1;\r\n\r\n    while(count&lt;20)\r\n    {\r\n        printf(\"%3d\",value);\r\n        value += direction;\r\n        if( value==5 || value==-5)\r\n            direction *= -1;\r\n        count++;\r\n    }\r\n    putchar('\\n');\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Variable <code>value<\/code> is the number output, swinging back and forth between -5 and 5; <code>count<\/code> is the number of values to generate; and <code>direction<\/code> is either 1 or -1, which flips when <code>value<\/code> reaches 5 or -5.<\/p>\n<p>Variable <code>direction<\/code> is initially set to 1 (positive) in Line 8. The statement <code>value+=direction<\/code> (Line 13) increments or decrements variable <code>value<\/code>. The <em>if<\/em> statement at Lines 14 and 15 toggles <code>direction<\/code> when <code>value<\/code> hits 5 or -5. The math is <code>direction*=-1<\/code>, which flips the variable between 1 and -1, back and forth.<\/p>\n<p>This example is just one of many types of toggles you can program in C. Also popular are bitwise toggles, where individual bits in a value represent certain conditions in a program. Such code tests the bits to determine whether a setting is on or off. I enjoy twiddling bits, mostly because of my Assembly language background. Because bit-twiddling involves using bitwise-logical operators, some C programmers prefer instead to use <em>char<\/em> variables set to 1 or 0.<\/p>\n<p>If you conjured a similar solution, great! Many potential resolutions of this puzzle exist, including those that would use <em>sin()<\/em> or some other <code>math.h<\/code> function to output similar results.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For my solution to this month&#8217;s Oscillation Exercise I created a toggle variable. This variable switches between 1 and -1. It sets the direction for the cascading values.<\/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-2948","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\/2948","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=2948"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2948\/revisions"}],"predecessor-version":[{"id":2965,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2948\/revisions\/2965"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2948"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2948"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2948"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}