title: Segmentation fault error 11 C++
body: <p>So I'm getting  a segmentation fault error in the beginning of the code. I've tried running some tests at the different points and the error seems to be when i allocate memory for the array. Ive just started learning about heap and stack memory so I'm not really sure if I'm doing something wrong there. Any help would be appreciated.</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  //Function Prototypes
  void sort(int A[], int n);
  int findMin(int A[], int n, int j);
  int swap(int&amp; a, int&amp; b);
  double median(int A[], int n);
  void output1(int median);
  void output2(double median);
  
  int main()
  {
    int size;
    int array[size]; //Segmentaion fault here
    int i = 0;
  
    cout &lt;&lt; "Enter the size of the list (&lt; 1 to quit): ";
    cin &gt;&gt; size;
  
    while(size &gt;= 1)
      {
        double element;
  
        cout &lt;&lt; "Enter element " &lt;&lt; i+1 &lt;&lt; ": ";
        cin &gt;&gt; element;
  
        array[i] = element;
  
        i++;
  
        while(i &lt; size)
      {
        cout &lt;&lt; "Enter element " &lt;&lt; i+1 &lt;&lt; ": ";
        cin &gt;&gt; element;
  
        array[i] = element;
        i++;
      }
  
        sort(array, size);
        median(array, size);
  
         cout &lt;&lt; "Enter the size of the list (&lt; 1 to quit): ";
         cin &gt;&gt; size;
      } 
    delete [] array;
    return 0;
  
  }
  
  
  void sort(int A[], int n)
  {
    int min;
    for(int i = 0; i &lt; n; i++)
      {
        min = findMin(A,n,i);
        //min = findMinIndex(p, size, i);
  
        //if(min )
          swap(A[i],A[min]);
        //swap(p[i],p[min]);
      }
  }
  
  int findMin(int A[], int n, int j)
  {
    int minIndex = j;
    for(int i = j+1; i &lt; n; i++)
      if(A[i]&lt;A[minIndex])
        minIndex = i;
    return minIndex; 
  }
  
  int swap(int&amp; a, int&amp; b)
  {
    int temp;
    temp = a;
    a = b;
    b = temp;
  }
  
  void output1(int median)
  {
    cout &lt;&lt; "The median is " &lt;&lt; median &lt;&lt; "." &lt;&lt; endl;
  }
  
  void output2(double median)
  {
    cout &lt;&lt; "The median is " &lt;&lt; median &lt;&lt; "." &lt;&lt; endl;
  }
  
  
  double median(int A[], int n)
  {
  
  
    if(n % 2 == 0)
      {
        int div1 = n / 2;
        int num1 = A[div1];
        int num2 = A[div1 -1];
        double median = (num1 + num2) / 2;
        output2(median);
      }
    else
      {
        int div2 = n - 1;
        int median = div2 / 2;
        output1(median);
      }
  }
  </code></pre>