title: segmentation fault 11 in C++ code
body: <p>for my bucket sort, I don't know why the out put is segmentation fault: 11 when I try to print out sorted distance. It should be running well on win pc, but I try running it on mac, but i just get error. where am I wrong?</p>
  
  <pre><code>using namespace std;
  void bucketSort(vector&lt;double&gt; &amp;arr, int n)
  {
      vector&lt;double&gt; distance;
      vector&lt;double&gt; b[n];
  
      for (int i=1; i&lt;=n/2; i++)
      {
      double c = sqrt(i)/sqrt(n/2);
      cout&lt;&lt;c&lt;&lt;endl;
      }
  
      for (int i=0; i&lt;n; i++)
      {
      double dist = sqrt(pow((arr[i]-0), 2) + pow((arr[i+1]-0), 2));
      i = i+1;
      distance.push_back(dist);
      }
  
      for (int i=0; i&lt;n; i++)
      {
      int bi = n*distance[i]; // Index in bucket
         b[bi].push_back(distance[i]);
      }
  
      for (int i=0; i&lt;n; i++)
      {
      sort(b[i].begin(), b[i].end());
      }
  
      int index = 0;
      for (int i = 0; i &lt; n; i++)
      {
      for (int j = 0; j &lt; b[i].size(); j++)
      {
          distance[index++] = b[i][j];
      }
      }
  
      for (int i = 0; i &lt; n/2; i++)
      {
      cout&lt;&lt;distance[i]&lt;&lt;endl;
      }
  }
  int main()
  {
      vector&lt;double&gt; A;
  
      double numbers;
  
      while (cin&gt;&gt;numbers)
      {
      A.push_back(numbers);
      }
  
      int n = A.size();
      bucketSort(A, n);
  }
  </code></pre>
  
  <p>Here is the out put:</p>
  
  <pre><code>Claudes-MBP:desktop BengDai$ ./a.out &lt; n.txt
  0.5
  0.707107
  0.866025
  1
  Segmentation fault: 11
  Claudes-MBP:desktop BengDai$ 
  </code></pre>