Getting a memory address in my array?
<p>I'm writing a C++ application that has a user enter a 0 (zero) or a one (1) as input, then stores the numbers as an array and sorts them so that the zeros come first and the ones come last.</p>

<p>However, I think I'm getting a memory address in my array that's messing up the sorting operation.</p>

<p>The function that accepts input looks like this:</p>

<pre><code>cout &lt;&lt; "Please enter number " &lt;&lt; i+1 &lt;&lt; ":\n";
cin &gt;&gt; ar[i];
</code></pre>

<p>Then there's a function that's called that sorts the input and displays the sorted list:</p>

<pre><code>sort_elements(ar, number);
</code></pre>

<p>... and that function looks like this:</p>

<pre><code>void sort_elements(int ar[], long int num_elements) {
    int temp_num;
    num_elements -= 1; //since the array starts at 0

    cout &lt;&lt; "num_elements is " &lt;&lt; num_elements &lt;&lt; "\n";
    for (int i=0; i &lt; (num_elements/2); i++ ) {
        if (ar[i] &gt; ar[num_elements-i]) {
            temp_num = ar[i];
            ar[i] = ar[num_elements-i];
            ar[num_elements-i] = temp_num;
        }
    }   
    cout &lt;&lt; "Here's your neatly sorted list of numbers: \n";
    for (int j=0; j &lt;= num_elements; j++) {
        cout &lt;&lt; ar[j] &lt;&lt; ", ";   
    }
    cout &lt;&lt; "\n";   
}
</code></pre>

<p>For a five number input, starting with three "1"s, and ending with two "0"s, this results in an output that looks like this:</p>

<pre><code>1, 0, 1, 1, 1892218304, 
</code></pre>

<p>I'm assuming the <code>1892218304</code> is a memory address, that's messing up the input. Though I don't really know.</p>

<p>Can anyone figure out why my sort operation is getting messed up?</p>

<p>Thanks in advance.</p>
