Segmentation fault error 11 C++

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.

#include <iostream>
using namespace std;

//Function Prototypes
void sort(int A[], int n);
int findMin(int A[], int n, int j);
int swap(int& a, int& 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 << "Enter the size of the list (< 1 to quit): ";
  cin >> size;

  while(size >= 1)
    {
      double element;

      cout << "Enter element " << i+1 << ": ";
      cin >> element;

      array[i] = element;

      i++;

      while(i < size)
    {
      cout << "Enter element " << i+1 << ": ";
      cin >> element;

Leave a Comment