Everything compiles no warnings or errors, but after I run this I get a core dump if I uncomment "delete [] a;" at the end of the main() function. It seems to run correctly if I leave "delete [] a;" commented out? Any suggestions will be appreciated.

#include <iostream>
using namespace std;

typedef unsigned int size_t;
size_t size = 0; // Initialize the size of the array
int* a = new int[size]; // Create a dynamic array

float Median(int* a, size_t size); // Median function declaration
float Mean(int* a, size_t size); // Mean function declaration
void Swap(int& x, int& y); // Swap function declaration
void Sort(int* a, size_t size); // Sort function declaration


int main()
{

  cout << "Enter integers (any character to quit): ";
  
  int n = 0;
   
  // Loop through user's integers
  while((std::cin >> n) && (size < 100))
  {

    a[size] = n; 
    size++;
 
  }

  // Output the Input Values
  cout << '\n';
  cout << "Input values:    ";
       
  if(size != 0)
    {
      for(size_t d=0;d<size;d++)
	{
	       
	  cout << a[d] << " ";
 
	}
    }

  // Output the Mean
  cout << '\n';
  cout << "Mean:    " << Mean(a, size) << '\n'; 

  // Output the Median
  cout << "Median:  " << Median(a, size) << '\n';

  // Output the Sorted Input Values
  cout << "Sorted input values:  ";
  if(size != 0)
    {
      for(size_t e=0;e<size;e++)
	{

	  cout << a[e] << " ";

	}
    }

  cout << '\n';

  // delete the array (I get a core dump error when uncommented)
  //delete [] a;

  return 0;

} // End of main


// Calculate the Mean of the array, function body of Mean
float Mean(int* a, size_t size)
{

  float mean;
  if(size != 0)
    {

      float sum = 0;
      for(size_t i=0;i<size;++i)
	{

	  sum += a[i];
       
	}

      mean = sum/size;

    }
  else
    {

      mean = 0;

    }

  return mean;

} // End of function Mean


// Calculate the Median of the array, function body of Median
float Median(int* a, size_t size)
{

  // Sort the array from smallest to largest
  if(size != 0)
    {
      Sort(a, size);
    }

  float median;


  if((size%2 == 0) && (size != 0))
    {

      // median if size is even
      median = ((a[size/2] + a[size/2-1])/2.0F);
      
    }
  else if(size != 0)
    {

      // median if size is odd
      median = a[(size-1)/2];
      
    }
  else
    {

      median = 0;

    }

  return median;

}


// Sort the array
void Sort(int* a, size_t size)
{

  for(size_t i = 0;i<size-1;i++)
    {
      int k = i;
      for(size_t j=i+1;j<size;j++)
	{

	  if(a[j] < a[k])
	    {
	      k = j;
	    }

	}
      
    // Call function Swap
    Swap(a[i], a[k]);

    }

} // End function Sort

// Swap Values in array
void Swap(int& x, int& y)
{

  int temp = x;
  x = y;
  y = temp;

}
// ...
size_t size = 0; // Initialize the size of the array
int* a = new int[size]; // Create a dynamic array *** of size 0? ***
// ...

Thanks. I guess in C++ you can't resize a dynamic array like I was trying to.

You need to specify the max size up front. const int maxSize = 100; Then int* a = new int[maxSize]; // Create a dynamic array Then while((std::cin >> n) && (size < maxSize)) Though to be honest, you should really start with a std::vector<int> a(maxSize); which takes care of all that memory management stuff for you.

Thanks for the information. We haven't used vectors yet. From what I've read in these forums that's the best way to go.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.