{"id":4588,"date":"2021-02-01T00:01:43","date_gmt":"2021-02-01T08:01:43","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4588"},"modified":"2021-02-11T12:41:43","modified_gmt":"2021-02-11T20:41:43","slug":"a-pair-of-arrays","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4588","title":{"rendered":"A Pair of Arrays"},"content":{"rendered":"<p>Removing redundancies from your C code may not be your first goal, but it&#8217;s something you can definitely find later. One example is when you must initialize a pair of arrays. Why use two loops when one will do?<br \/>\n<!--more--><br \/>\nFor this month&#8217;s Exercise, your challenge is to initialize two 50-element arrays, <code>alpha[]<\/code> and <code>beta[]<\/code>. Sounds simple, but array <code>alpha[]<\/code> is set to values 1 through 50 while array <code>beta[]<\/code> is set to values 51 through 100. Here is sample output showing each array&#8217;s values:<\/p>\n<pre class=\"screen\">\r\n 1\t51\r\n 2\t52\r\n 3\t53\r\n 4\t54\r\n 5\t55\r\n 6\t56\r\n 7\t57\r\n 8\t58\r\n 9\t59\r\n10\t60\r\n...\r\n40\t90\r\n41\t91\r\n42\t92\r\n43\t93\r\n44\t94\r\n45\t95\r\n46\t96\r\n47\t97\r\n48\t98\r\n49\t99\r\n50\t100<\/pre>\n<p>If you believe that this Exercise sounds simple, try performing the initialization using only one loop. Further, use a single statement to assign the values to both array elements. Remember, the goal here is to optimize code. A single loop can easily initialize the two arrays, but a single statement?<\/p>\n<p><a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4596\">Click here<\/a> to view my solution, but please try this challenge on your own before you peek to see what I&#8217;ve done.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s possible to initialize two arrays with different values in the same loop &#8211; but only when you&#8217;re clever. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4588\">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-4588","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\/4588","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=4588"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4588\/revisions"}],"predecessor-version":[{"id":4623,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4588\/revisions\/4623"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4588"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4588"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4588"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}