{"id":5767,"date":"2023-03-01T00:01:35","date_gmt":"2023-03-01T08:01:35","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5767"},"modified":"2023-03-09T20:54:25","modified_gmt":"2023-03-10T04:54:25","slug":"merging-arrays","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5767","title":{"rendered":"Merging Arrays"},"content":{"rendered":"<h2>Difficulty: &#9733; &#9733; &#9734; &#9734;<\/h2>\n<p>You have a number of options for merging values between two arrays. I suppose the solution you devise depends on how you interpret the word &#8220;merge.&#8221;<br \/>\n<!--more--><br \/>\nFor example, you could just stick one array onto the end of another:<\/p>\n<p><code>Array 'a': 22 41 92 67 16<br \/>\nArray 'b': 34 16 57 46 22<br \/>\n'a' + 'b': 22 41 92 67 16 34 16 57 46 22<\/code><\/p>\n<p>You could create a single array and eliminate any overlap between the two, snipping out the duplicate values:<\/p>\n<p><code>Array 'a': 22 41 92 67 16<br \/>\nArray 'b': 34 16 57 46 22<br \/>\n'a' + 'b': 22 41 92 67 16 34 57 46<\/code><\/p>\n<p>Above, the values 22 and 16 appear only once in the result.<\/p>\n<p>You can also sort the arrays to present the concatenated result sequentially. But for this month&#8217;s Exercise, I&#8217;d like you to zipper the arrays together. This color-coded sample output saving me many words of text:<\/p>\n<p><code><span style=\"color:green\">Array 'a': 22 41 92 67 16<\/span><br \/>\n<span style=\"color:orange\">Array 'b': 34 16 57 46 22<\/span><br \/>\n<span style=\"color:green\">'a'<\/span> + <span style=\"color:orange\">'b'<\/span>: <span style=\"color:green\">22<\/span> <span style=\"color:orange\">34<\/span> <span style=\"color:green\">41<\/span> <span style=\"color:orange\">16<\/span> <span style=\"color:green\">92<\/span> <span style=\"color:orange\">57<\/span> <span style=\"color:green\">67<\/span> <span style=\"color:orange\">46<\/span> <span style=\"color:green\">16<\/span> <span style=\"color:orange\">22<\/span><\/code><\/p>\n<p>I call it a &#8220;zipper&#8221; because the values from each original array alternate in the result, as shown above.<\/p>\n<p>Your task for this month&#8217;s challenge is to write a <em>merge()<\/em> function that swallows two arrays of equal size. It returns a new array that contains values from the two original arrays, but zippered together. (You don&#8217;t have to code color output.)<\/p>\n<p>Here is a skeleton to help get you started:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2023_03_01-Lesson.c\" rel=\"noopener\" target=\"_blank\">2023_03_01-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;time.h&gt;\r\n\r\n#define SIZE 5\r\n\r\n<span class=\"comments\">\/* merge function goes here *\/<\/span>\r\n\r\nint main()\r\n{\r\n    int x,a[SIZE],b[SIZE],*c;\r\n\r\n    <span class=\"comments\">\/* seed the randomizer *\/<\/span>\r\n    srand( (unsigned)time(NULL) );\r\n\r\n    <span class=\"comments\">\/* populate the arrays, values 0 through 99 *\/<\/span>\r\n    for( x=0; x&lt;SIZE; x++ )\r\n    {\r\n        a[x] = rand()%100;\r\n        b[x] = rand()%100;\r\n    }\r\n\r\n    <span class=\"comments\">\/* output the arrays *\/<\/span>\r\n    printf(\"Array 'a': \");\r\n    for( x=0; x&lt;SIZE; x++ )\r\n        printf(\" %2d\",a[x]);\r\n    putchar('\\n');\r\n    printf(\"Array 'b': \");\r\n    for( x=0; x&lt;SIZE; x++ )\r\n        printf(\" %2d\",b[x]);\r\n    putchar('\\n');\r\n\r\n    <span class=\"comments\">\/* call the merge() function *\/<\/span>\r\n    <span class=\"comments\">\/* output the merged arrays *\/<\/span>\r\n\r\n    return(0);\r\n}\r\n<\/pre>\n<p>Your <em>merge()<\/em> function sits where the comment is at Line 7 in the code. After the two original arrays <code>a[]<\/code> and <code>b[]<\/code> are stuffed with random values and output, the <em>merge()<\/em> function is called. Then, in the <em>main()<\/em> function, output the merged arrays, as shown earlier.<\/p>\n<p>Please try this Exercise on your own before you peek at <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5783\">my solution<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Don&#8217;t just stick one array onto the other, zipper them together! <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5767\">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-5767","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\/5767","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=5767"}],"version-history":[{"count":11,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5767\/revisions"}],"predecessor-version":[{"id":5801,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5767\/revisions\/5801"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5767"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5767"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5767"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}