{"id":4596,"date":"2021-02-08T00:01:15","date_gmt":"2021-02-08T08:01:15","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4596"},"modified":"2021-02-11T12:40:52","modified_gmt":"2021-02-11T20:40:52","slug":"a-pair-of-arrays-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4596","title":{"rendered":"A Pair of Arrays &#8211; Solution"},"content":{"rendered":"<p>The challenge for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4588\">this month&#8217;s Exercise<\/a> is to initialize two different arrays to two sets of values. You should try to use only one loop and try to use a single statement to make the element assignments.<br \/>\n<!--more--><br \/>\nThe first array <code>alpha[]<\/code> is initialized to values 1 through 50.<\/p>\n<p>The second array <code>beta[]<\/code> is initialized to values 51 through 100.<\/p>\n<p>The good news is that scant math is required to initialize the two arrays together; <code>beta[]<\/code>&#8216;s elements are the same as <code>alpha[]<\/code>&#8216;s plus 50. So a single loop that repeats 50 times can easily fill both arrays, as shown in this solution, which uses two statements in a <em>for<\/em> loop to initialize the element values:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2021_02-Exercise.c\" rel=\"noopener\" target=\"_blank\">2021_02-Exercise.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\nint main()\r\n{\r\n    char alpha[50], beta[50];\r\n    int x;\r\n\r\n    <span class=\"comments\">\/* initialize the arrays *\/<\/span>\r\n    for( x=0; x&lt;50; x++)\r\n    {\r\n        alpha[x] = x+1;\r\n        beta[x] = x+51;\r\n    }\r\n\r\n    <span class=\"comments\">\/* output results *\/<\/span>\r\n    for( x=0; x&lt;50; x++)\r\n        printf(\"%2d\\t%2d\\n\",alpha[x],beta[x]);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The <em>for<\/em> loop at Line 10 initializes both arrays:<\/p>\n<p><code>alpha[x] = x+1;<br \/>\nbeta[x] = x+51;<\/code><\/p>\n<p>Because each element differs by 50, the math works on variable <code>x<\/code> to assign values 1 through 50 to <code>alpha[]<\/code> and 51 through 100 to <code>beta[]<\/code>. A <em>printf()<\/em> statement in a <em>for<\/em> loop at Line 17 outputs the results in two columns.<\/p>\n<p>To make the assignments in a single statement, replace Lines 12 and 13 in my solution with this:<\/p>\n<p><code>beta[x] = (alpha[x] = x+1) + 50;<\/code><\/p>\n<p>Working from the inside out, the assignment is first made to <code>alpha[x]<\/code>. The result is added to 50, then assigned to <code>beta[x]<\/code>. The program&#8217;s output is the same.<\/p>\n<p>I hope you enjoyed this Exercise, and that your solution generated the proper output. More power to you if you made the assignments in both a single loop and single statement.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The challenge for this month&#8217;s Exercise is to initialize two different arrays to two sets of values. You should try to use only one loop and try to use a single statement to make the element assignments.<\/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-4596","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\/4596","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=4596"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4596\/revisions"}],"predecessor-version":[{"id":4622,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4596\/revisions\/4622"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4596"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4596"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4596"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}