Not logged in. Log in with GitHub. About Websheets.

List of all available Websheets


Viewing cpp/arrays/mergearrays by redekopp@usc.edu. You have unsaved changes. Log in above to create, edit, preview and publish Websheets.
PropertyValue
Description
html markup
shown to student
 
Complete the function <tt>merge()</tt> to merge two arrays of 
length <tt>n</tt>, passed as <tt>src1</tt> and <tt>src2</tt> into
an output array <tt>dest</tt>.  You should alternate items from the
src1 array and the src2 array.  
<br>For example is the src1 array and src2 array contents are<br>
<pre>
1 3 5
2 4 6
</pre>
then the dest array should contain
<pre>
1 2 3 4 5 6
</pre>
Public permissions remove
Remarks
Comments, history, license, etc.
 
Originally by Mark Redekopp (redekopp@usc.edu) and Dave Pritchard (daveagp@gmail.com)
remove
Engine
Template / Reference solution
 
#include <iostream>
using namespace std;
void merge(int src1[], int src2[], int len, int dest[])
{
\[
  int oi = 0;
  for(int i=0; i < len; i++){
    dest[oi++] = src1[i];
    dest[oi++] = src2[i];
  }
]\
}
int main() {
   int a1[] = {1, 3, 5};
   int a2[] = {2, 4, 6};
   int a3[6];
 
   // These are 0-length arrays to make sure you coded things correctly
   int *a4 = NULL, *a5 = NULL, *a6 = NULL;
   
   // test your function
   merge(a1, a2, 3, a3);
   for(int i=0; i < 6; i++){
     cout << a3[i] << " ";  
   }
   cout << endl;
   // pass in 0-length arrays
   merge(a4, a5, 0, a6);  
  
   
   return 0;
}
C++ test suite
json list of stdin/args tests
e.g. [{"stdin":"hi", "args":["4", "5"]},
{"stdin":"noargs"}]

to just run once with no input use [{}]
 
[
   {}
]
Solution visibility remove


Optional properties:

Note: problems are open-source by default (see 'Public permissions'). Assumed license for open problems is Creative Commons 4.0 Attribution-ShareAlike unless specified in 'Remarks'.