title: How to fix output display so it shows correctly sorted data
body: <p>When I call my print function, my program displays the unsorted data after I've ran it through bubble sort. I've compared my code to friends and an article about bubble sort I found on geeksforgeeks (<a href="https://www.geeksforgeeks.org/bubble-sort/" rel="nofollow noreferrer">https://www.geeksforgeeks.org/bubble-sort/</a>) and for whatever reason my data won't sort.</p>
  
  <p>I'm writing a program for my college data structures class with 4 sorting functions. What is supposed to happen in theory is the program reads in a data file with 8000 random numbers and sorts the data based on which sort is called (bubble sort, insert sort, selection sort, and quick sort). A print function is called and will display every 1000th data element to show that the data is indeed sorted. I've tried coding my print in main, putting the print call in the sorting function, and using a function to print, Quadruple checked my sorting function. I've also tried different compilers and a different computer but I suspect it's something funky with my code.</p>
  
  <h2>Original code:</h2>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  #include &lt;cmath&gt;
  #include &lt;iomanip&gt;
  using namespace std;
  const int arraysize = 8000;
  int numcompares, numcopies;
  void swapem(double a, double b)
  {
      double temp;
      temp = a;
      a = b;
      b = temp;
  }
  void bubblesort(double r[], int n)
  {
      int j, i;
      for (j = 0; j &lt; n - 1; j++)
      {
          for (i = 0; i &lt; n - 1; i++)
          {
              numcompares++;
              if (r[i] &gt; r[i + 1])
              {
                  swapem(r[i], r[i + 1]);
                  numcopies += 3;
              }
          }
      }
  }
  void printem(double r[])
  {
      cout &lt;&lt;  r[1000] &lt;&lt; ", " &lt;&lt; r[2000] &lt;&lt; ", " &lt;&lt; r[3000] &lt;&lt; ", " &lt;&lt;
      r[4000] &lt;&lt; ", " &lt;&lt; r[5000] &lt;&lt; ", " &lt;&lt; r[6000] &lt;&lt; ", " &lt;&lt; r[7000] &lt;&lt; ", " &lt;&lt; r[7999] &lt;&lt; endl;
  }
  int main()
  {
      ifstream inf("data.dat");
      ofstream outf("sorted.ot");
      string sortname;
      double arraynums[arraysize];
      for (int i = 0; i &lt; arraysize; i++)
      {
          inf &gt;&gt; arraynums[i];
      }
      bubblesort(arraynums, arraysize);
          system("pause");
      return 0;
  }
  </code></pre>
  
  <hr>
  
  <h2>I've tried:</h2>
  
  <pre><code>void printem(double r[], int n)
  {
      int i;
      for (i = 0; i &lt; n; i++)
          cout &lt;&lt; r[i]&lt;&lt; ", ";    
  }
  printem(arraynums,20); // in main
  </code></pre>
  
  <hr>
  
  <h2>and in main</h2>
  
  <pre><code>for (int i = 0; i &lt; 10; i++)
      {
          cout &lt;&lt; arraynums[i] &lt;&lt; endl;
      } 
  
  </code></pre>
  
  <hr>
  
  <h2>Here's 10 of the 8000 random integers that refuse to be sorted</h2>
  
  <pre><code>41275.4
  12113.1
  50676
  7662.34
  50688.3
  -7926.28
  13672.8
  -3212.9
  -13046.5
  -16798
  </code></pre>
  
  <p>The output should be:
  -16798
  -13046.5
  -7926.28
  -3212.9
  7662.34
  12113.1
  13672.8
  41275.4
  50676
  50688.3</p>
  
  <p>Yet it stays in it's unsorted form:
  41275.4
  12113.1
  50676
  7662.34
  50688.3
  -7926.28
  13672.8
  -3212.9
  -13046.5
  -16798</p>