{"id":2941,"date":"2018-02-01T00:01:05","date_gmt":"2018-02-01T08:01:05","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2941"},"modified":"2018-02-08T11:29:53","modified_gmt":"2018-02-08T19:29:53","slug":"oscillation","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2941","title":{"rendered":"Oscillation"},"content":{"rendered":"<p>Going back and forth happens frequently in the real world, and not just when you forget to pick up something at the store. Pendulums swing back and forth. The tide rises and falls. Politicians waffle.<br \/>\n<!--more--><br \/>\nIn C, you can use the <em>sin()<\/em> function to obtain the natural sinus rhythm:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;math.h&gt;\r\n\r\nint main()\r\n{\r\n    float x;\r\n\r\n    for(x=0.0;x&lt;20.0;x+=0.5)\r\n        printf(\"%f\\n\",\r\n                sin(x)\r\n              );\r\n\r\n    return(0);\r\n}<\/pre>\n<p>This code&#8217;s output displays values cascading from 0.0 to 1.0, back to 0.0, down to -1.0, and back up again. Thanks to mathematics I&#8217;m too impatient to explain, the values naturally switch directions; only one variable is needed in the code because the <em>sin()<\/em> function outputs the result within the range of -1.0 to 1.0.<\/p>\n<p>You can emulate this type of behavior in your code. For example, say you need code that generates integer values between -5 and 5, cycling over and over. Here&#8217;s sample output:<\/p>\n<pre><code> 0  1  2  3  4  5  4  3  2  1  0 -1 -2 -3 -4 -5 -4 -3 -2 -1\r\n 0  1  2  3  4  5  4  3  2  1  0 -1 -2 -3 -4 -5 -4 -3 -2 -1 \r\n 0  1  2  3  4  5  4  3  2  1<\/code><\/pre>\n<p>Values to up to 5, then march down to -5 and back up to 5 again, over and over.<\/p>\n<p>For this month&#8217;s Exercise, your task is to write code that outputs such an oscillating pattern of integer values. You can try to use trigonometric functions, but an easier solution is possible. Start at zero and bounce the output between 5 and -5, generating 20 numbers (as shown above).<\/p>\n<p>View <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2948\">my solution here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Back and forth and back and forth. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2941\">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":[3],"tags":[],"class_list":["post-2941","post","type-post","status-publish","format-standard","hentry","category-exercise"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2941","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=2941"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2941\/revisions"}],"predecessor-version":[{"id":2970,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2941\/revisions\/2970"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2941"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2941"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2941"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}