{"id":2840,"date":"2017-12-01T00:01:39","date_gmt":"2017-12-01T08:01:39","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2840"},"modified":"2017-12-08T08:58:50","modified_gmt":"2017-12-08T16:58:50","slug":"swapping-arrays","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2840","title":{"rendered":"Swapping Arrays"},"content":{"rendered":"<p>I&#8217;m amazed, and a bit envious, when I look at other programming languages to discover a rich variety of functions, or &#8220;methods,&#8221; that do specific tasks the C language lacks. One of these tasks deals with arrays.<br \/>\n<!--more--><br \/>\nArrays in C are quite primitive, which is to be expected for a medium-level programming language. In other, higher languages, plenty of tools available to manipulate the contents of an array or report specific aspects.<\/p>\n<p>For example, you can sort an array, pluck out the minimum or maximum values, collect subsets, reverse the contents, add and remove elements (dynamically), and more. All those actions are available to the array data type without having to code your own functions. Of course, one of the things I enjoy about C is that it forces you to code your own functions for these tasks. The process helps train your programming kung fu.<\/p>\n<p>Suppose that one of the functions available in another programming language swaps the contents of two arrays. In the following code, you see two arrays, <code>a<\/code> and <code>b<\/code>. Each has the same number of elements, but with different values:<\/p>\n<pre class=\"screen\">\r\nint main()\r\n{\r\n    int a[SIZE] = { 2, 4, 6, 8, 10 };\r\n    int b[SIZE] = { 1, 3, 5, 7, 9 };\r\n\r\n    printf(\"Original arrays:\\n\");\r\n    show_arrays(a,b);\r\n\r\n    swap_arrays(a,b);\r\n\r\n    printf(\"After swap:\\n\");\r\n    show_arrays(a,b);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Two functions are referenced in the code snippet: <em>show_arrays()<\/em> and <em>swap_arrays()<\/em>. Your task for this month&#8217;s Exercise is to write these functions. This process isn&#8217;t particularly tricky, so don&#8217;t believe that I&#8217;m throwing you a curve; displaying and swapping arrays are activities that must be hand-coded in C.<\/p>\n<p><a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2847\">Click here<\/a> to view my solution.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Write functions that display array values and swap their values. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2840\">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-2840","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\/2840","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=2840"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2840\/revisions"}],"predecessor-version":[{"id":2867,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2840\/revisions\/2867"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2840"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2840"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2840"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}