Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing an array as an argument in C++
    text
    copied!<p>I'm writing a merge sort function, and right now I am just using a test case array (there is no input - this is static, for now). I don't know how to pass an array as an argument. Here is my code right now:</p> <pre><code>//merge sort first attempt #include &lt;iostream&gt; #include &lt;algorithm&gt; #include &lt;vector&gt; int mergeSort(int[]); int main() { int originalarray[] = {1, 3, 5, 7, 9, 2, 4, 6, 8, 10}; mergeSort(originalarray[]); } int mergeSort(int[] originalarray) { int num = (sizeof(originalarray)/sizeof(int)); std::vector&lt;int&gt; original(num); if (num &gt; 2) { return num; } // Fill the array using the elements of originalarray // This is just for demonstration, normally original will be a parameter, // so you won't be filling it up with anything. std::copy(originalarray, originalarray + num, original.begin()); // Create farray and sarray of the appropriate size std::vector&lt;int&gt; farray(num / 2); std::vector&lt;int&gt; sarray(num - farray.size()); // Fill those using elements from original std::copy(original.begin(), original.begin() + farray.size(), farray.begin()); std::copy(original.begin() + farray.size(), original.end(), sarray.begin()); mergeSort(farray); mergeSort(sarray); </code></pre> <p>}</p> <p>Note that this mergeSort function is not functional, as I have not figured out how to merge them yet (that's my assignment). I would like to get my two vectors sorted before I deal with that, and I can't compile this because of my need to pass an array as an argument. I don't understand pointers, so if that is the solution, my excuse is ignorance. I'm learning programming right now, with C++ as a first language, and only have a basic grasp of the language's features. Thanks for the help.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload