Trying to generate an array with random ints and place into a merge sort function. What am I doing wrong?

Viewed 41

I have developed a random array generator function to place into a selection sort, and it's working. But when I put it into a merge sort, it seems to break. What's going on? If I initalize my array with numbers that I input, it seems to work fine.

Here's the Error that pops up:

times accessed array: 1
times accessed array: 1
times accessed array: 1
[1]    44682 abort    

Here's my main:

int main()
{
      int array[8];
      randArrayGenerator(array, 8);
      mergeSort(array,0,8);
      printArray(array,8);
}

the random array generator function I wrote:


void randArrayGenerator(int arr[], int size)
{
      srand(time(0));

      for(int i = 0; i < size; i++)
      {
            arr[i] = (1 + rand() % 100);
      }
}

merge sort (works when I put in values myself):

template <class ItemType>
void mergeSort(ItemType theArray[], int first, int last)
{
      if (first < last)
      {
            // sort each half
            // index of midpoint
            int mid = first + (last - first) / 2;
            
            // sort left half theArray[first..mid]
            mergeSort(theArray, first, mid);

            // sort right half theArray[mid+1..last]
            mergeSort(theArray, mid + 1, last);

            // merge two halves
            merge(theArray, first, mid, last);
      }
}

the print array function:


template <class ItemType>
void printArray(ItemType theArray[], int n)
{
      int i;

      for (int i = 0; i < n; i++)
      {
            cout << theArray[i] << " ";
            cout << endl;
      }
}

0 Answers
Related