{"id":2847,"date":"2017-12-08T00:01:22","date_gmt":"2017-12-08T08:01:22","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2847"},"modified":"2017-12-02T11:39:57","modified_gmt":"2017-12-02T19:39:57","slug":"swapping-arrays-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2847","title":{"rendered":"Swapping Arrays &#8211; Solution"},"content":{"rendered":"<p>The two functions required from <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2840\">this month&#8217;s Exercise<\/a> are <em>show_arrays()<\/em> and <em>swap_arrays()<\/em>. They&#8217;re not that difficult to code, meaning that you don&#8217;t need to use the dratted <code>**<\/code> notation or perform any special tricks. Here&#8217;s how I solved the problem:<br \/>\n<!--more--><br \/>\nFirst, I used a <code>SIZE<\/code> constant to handle the number of elements in both arrays:<\/p>\n<p><code>#define SIZE 5<\/code><\/p>\n<p>This step isn&#8217;t required, but it does allow me flexibility later should both arrays change their number of elements.<\/p>\n<p>Second, for each function, the values passed are pointers, i.e., the memory address where each array is located. The functions are prototyped similarly:<\/p>\n<p><code>void show_arrays(int *y, int *z);<br \/>\nvoid swap_arrays(int *y, int *z);<\/code><\/p>\n<p>Both are <code>void<\/code> functions because they don&#8217;t return anything; the arrays are manipulated in memory directly, which is the beauty of using pointers.<\/p>\n<p>Third, the functions:<\/p>\n<p>The <em>show_arrays()<\/em> function is necessary because otherwise I&#8217;d repeat the same chunk of code in the <em>main()<\/em> function. It uses two <em>for<\/em> loops to display each array&#8217;s elements:<\/p>\n<pre class=\"screen\">\r\nvoid show_arrays(int *y, int *z)\r\n{\r\n    int x;\r\n\r\n    printf(\"a =\");\r\n    for(x=0;x&lt;SIZE;x++)\r\n        printf(\" %2d\",y[x]);\r\n    putchar('\\n');\r\n    printf(\"b =\");\r\n    for(x=0;x&lt;SIZE;x++)\r\n        printf(\" %2d\",z[x]);\r\n    putchar('\\n');\r\n}<\/pre>\n<p>The <code>%2d<\/code> placeholder in each <em>printf()<\/em> statement keeps the output looking even between both arrays:<\/p>\n<pre><code>a =  2  4  6  8 10\r\nb =  1  3  5  7  9<\/code><\/pre>\n<p>For the <em>swap_arrays()<\/em> function, here&#8217;s what I concocted:<\/p>\n<pre class=\"screen\">\r\nvoid swap_arrays(int *y, int *z)\r\n{\r\n    int x,temp;\r\n\r\n    for(x=0;x&lt;SIZE;x++)\r\n    {\r\n        temp = y[x];\r\n        y[x] = z[x];\r\n        z[x] = temp;\r\n    }\r\n}<\/pre>\n<p>All you need to swap elements is a temporary storage <em>int<\/em> variable, <code>temp<\/code>. The value of <code>y[x]<\/code> element is saved to <code>temp<\/code>. Then <code>z[x]<\/code> is placed into <code>y[x]<\/code>. Finally, <code>temp<\/code> is placed into <code>z[x]<\/code>. This loop repeats for each element.<\/p>\n<p>None of the variables in the function need to be pointers; only the array&#8217;s location is passed to the function as a pointer. Within the function, array notation can be used as a substitute. The <code>temp<\/code> variable isn&#8217;t a pointer because the array elements are <em>int<\/em> values, not pointers.<\/p>\n<p><a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2017\/11\/12exercise.c\">Click here<\/a> to view the final code, which I find quite simple, but still a good exercise.<\/p>\n<p>If you came up with another solution and the output is solid, congratulations!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The two functions required from this month&#8217;s Exercise are show_arrays() and swap_arrays(). They&#8217;re not that difficult to code, meaning that you don&#8217;t need to use the dratted ** notation or perform any special tricks. Here&#8217;s how I solved the problem:<\/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-2847","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\/2847","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=2847"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2847\/revisions"}],"predecessor-version":[{"id":2865,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2847\/revisions\/2865"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2847"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2847"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2847"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}