title: Sort numbers from smallest to biggest using vector instead of array in C++
body: <p>So I am new to C++ and I am working on this problem that asks me to sort numbers with the insertion sort using vectors instead of arrays. I have worked sorting using the array and then I just made some minor changes in the code trying to solve the problem with vector. Here's what I've got now:</p>
  
  <pre><code>#include &lt;cstdlib&gt;
  #include &lt;iostream&gt;
  #include &lt;vector&gt;
  
  using namespace std;
  
  void fill_array(vector&lt;int&gt; v,int size, int&amp; number_used);
  
  void sort(vector&lt;int&gt; v, int number_used);
  
  void swap_values(int&amp; v1, int&amp; v2);
  
  int main()     
  {
      cout &lt;&lt; "This program sorts numbers from lowest to highest.\n";
  
      vector&lt;int&gt; sample;
      int number_used;
  
      fill_array(sample,10,number_used);
      sort(sample,number_used);
  
      cout &lt;&lt; "In sorted order the numbers are:\n";
      for (int index =0; index &lt; number_used; index++)
          cout &lt;&lt; sample[index] &lt;&lt; " ";
      cout &lt;&lt; endl;
  
      system("PAUSE");
      return 0;
  }
  
  
  void fill_array(vector&lt;int&gt; v, int size, int&amp; number_used)
  { 
           cout &lt;&lt; "This program sorts numbers from lowest to highest.\n";
           cout &lt;&lt; "Enter up to " &lt;&lt; size &lt;&lt; " nonnegative whole number.\n"
                &lt;&lt; "Mark the end of the list with a negative number.\n";
  
           int index = 0,next; 
           cin &gt;&gt; next;
           while ((next &gt;= 0) &amp;&amp; (index &lt; size))
           {
                 v[index]=next;
                 index++;
                 cin &gt;&gt; next;
           }
           number_used = index;
  }
  
  void sort(vector&lt;int&gt; v, int number_used)
  {
       int index;
       int index_backwards;
  
      for(index=0;index&lt;number_used;index++)
      {
         for(index_backwards=index;index_backwards&gt;0;index_backwards--)
         {
             if(v[index_backwards-1]&gt;v[index_backwards])
             {
                 swap_values(v[index_backwards-1], v[index_backwards]);
             }
         }
      }
  }
  
  
  void swap_values(int&amp; v1, int&amp; v2)
  {
       int temp;
       temp = v1;
       v1=v2;
       v2=temp;
  }
  </code></pre>
  
  <p>It went through the compile just fine. But when i run it, after i had input the numbers and hit enter, the program just stops running and shuts down. Is there anyone could help me fix the problem? Thanks.</p>