title: C++ Bubble Sort Using Swap Function and Pointers
body: <p>I'm having problems figuring out where my bubble sort code went wrong. I'm almost positive it's in the sorting algorithm. Here is what I need my code to accomplish:</p>
  
  <p>-Initialize an array a fill it with random numbers (I use getNumbers() to accomplish this)</p>
  
  <p>-Compare the first element with all later elements. If the first element is larger, swap them.</p>
  
  <p>-Compare the second element with all later elements one by one. If the second element is larger, swap them.</p>
  
  <p>-Continue comparing and swap operations until the second to last element.</p>
  
  <p>-Print out the sorted array</p>
  
  <p>And here's my code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  
  using namespace std;
  
  void swap(int *, int *);
  
  int *getNumbers(int);
  
  int main()
  {
      //Get the size of the array from keyboard
      int arraySize;
      cout &lt;&lt; "How many integers would you like to declare: ";
      cin &gt;&gt; arraySize;
  
      //Initialize array  
      int *array;
      array = getNumbers(arraySize); //getNumbers should return a pointer
      //Print out original array
      cout &lt;&lt; "Original array" &lt;&lt; endl;
      for(int count = 0; count &lt; arraySize; count++)
      {
          cout &lt;&lt; *(array + count) &lt;&lt; " ";
      }
      //Sort array using the swap function
      //Have a for loop to swap numbers one by one from min to max
          //Compare values using a second for loop
              //Swap values if former value is larger
              //swap(&amp;array[i],&amp;array[j]);
      for(int i = 0; i &lt; arraySize; i++)
      {
          for(int j = 0; j &lt; (arraySize - 1); j++)
          {
              if(array[j] &gt; array[j + 1])
              {
                  swap(&amp;array[i], &amp;array[j]);
              }
          }
      }
      //Print out sorted array
      cout &lt;&lt; "\nSorted Array" &lt;&lt; endl;
      for(int count = 0; count &lt; arraySize; count++)
      {
          cout &lt;&lt; *(array + count) &lt;&lt; " ";
      }
      return 0;
  }
  
  void swap(int *num1, int *num2)
  {
      //Keep record of original value of num1
      int tempNum;
      tempNum = *num1;
      *num1 = *num2;  //num1 value has been changed
      *num2 = tempNum;    //Fetch the original value of num1 and assign it to num2
  }
  
  int *getNumbers(int size)
  {
      int *array;
  
      array = new int[size];
  
      srand(time(0));
  
      for(int i = 0; i &lt; size; i++)
      {
          array[i] = rand() % 100;
      }
      return array;
  }
  </code></pre>