{"id":5783,"date":"2023-03-08T00:01:10","date_gmt":"2023-03-08T08:01:10","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5783"},"modified":"2023-03-04T09:22:09","modified_gmt":"2023-03-04T17:22:09","slug":"merging-arrays-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5783","title":{"rendered":"Merging Arrays &#8211; Solution"},"content":{"rendered":"<p>This month&#8217;s <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5767\">Exercise<\/a> is about coding a <em>merge()<\/em> function, which swallows two arrays and generates a third as output. The new array is &#8220;zippered&#8221; from the original arrays, alternating values from each.<br \/>\n<!--more--><br \/>\nMy <em>merge()<\/em> function solution involves two steps.<\/p>\n<p>First, allocate storage for the new array. Because both arrays are the same size; only one size value is passed as an argument:<\/p>\n<p><code>int *merge(int *a, int *b, int s)<\/code><\/p>\n<p>If the two arrays were different sizes, two size arguments are required, each paired with its array. Either way, storage is allocated for the new array, which is assumed to be an integer data type.<\/p>\n<p>Second, loop through the two arrays, packing the newly-allocated array with alternating values from each of the two passed arrays. Two variables help move through the arrays: <code>offset<\/code> for the new array and <code>index<\/code> for the two passed arrays. Here is my <em>merge()<\/em> function:<\/p>\n<pre class=\"screen\">\r\nint *merge(int *a, int *b, int s)\r\n{\r\n    int *r,offset,index;\r\n\r\n    <span class=\"comments\">\/* allocate maximum stroage *\/<\/span>\r\n    r = malloc( sizeof(int) * s * 2 );\r\n\r\n    if( r!=NULL )\r\n    {\r\n        offset = index = 0;\r\n        while( offset&lt;s*2 )\r\n        {\r\n            r[offset++] = *(a+index);\r\n            r[offset++] = *(b+index);\r\n            index++;\r\n        }\r\n    }\r\n    return(r);\r\n}<\/pre>\n<p>The function returns the address of <em>int<\/em> pointer <code>r<\/code>, which is allocated as the first step. Providing the result isn&#8217;t <code>NULL<\/code>, values from each of the passed arrays, <code>a<\/code> and <code>b<\/code>, are assigned to sequential offsets in the new array.<\/p>\n<p>Variable <code>offset<\/code> is incremented after each value is added to array <code>r<\/code>, but variable <code>index<\/code> is incremented only after processing both arrays <code>a<\/code> and <code>b<\/code>.<\/p>\n<p>When the function is complete, address <code>r<\/code> is returned, though in the <em>main()<\/em> function I don&#8217;t test for <code>NULL<\/code>. Instead, the following statements handle the function call and the output:<\/p>\n<pre class=\"screen\">\r\n    c = merge(a,b,SIZE);\r\n    printf(\"Merged: \");\r\n    for( x=0; x&lt;SIZE*2; x++ )\r\n        printf(\" %2d\",c[x]);\r\n    putchar('\\n');\r\n<\/pre>\n<p>Though variable <code>c<\/code> is declared as a pointer, and used in the <em>merge()<\/em> function call that way, I use array notation when <code>c<\/code>&#8216;s values are output in the loop. This type of conversion is okay, and it keeps with the use of the other arrays in the <em>main()<\/em> function.<\/p>\n<p><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2023_03-Exercise.c\" rel=\"noopener\" target=\"_blank\">Click here<\/a> to view the full code on GitHub. I hope your solution worked out well and that the new array shows the proper data in sequence, zippered by the <em>merge()<\/em> function.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This month&#8217;s Exercise is about coding a merge() function, which swallows two arrays and generates a third as output. The new array is &#8220;zippered&#8221; from the original arrays, alternating values from each.<\/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-5783","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\/5783","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=5783"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5783\/revisions"}],"predecessor-version":[{"id":5794,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5783\/revisions\/5794"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}