1

I have this completed code about the merge sort:

#include <iostream>
#include <iomanip>
using namespace std;

const int n = 8;
void merge(int *arr, int low, int high, int mid);
void mergesort(int *arr, int low, int high);
int main()
{
    int arr[n] = { 3,41,52,26,38,57,9,49 };
    mergesort(arr, 0, n - 1);
    for (int i = 0; i<n; i++)
    {
        cout << arr[i] << setw(3);
    }
    return 0;
}

void merge(int *arr, int low, int high, int mid)
{
    int i, j, k, temp[high - low + 1];
    i = low;
    j = mid + 1;
    k = 0;
    while (i <= mid && j <= high)
    {
        if (arr[i] <arr[j])
        {
            temp[k] = arr[i];
            k++;
            i++;
        }
        else
        {
            temp[k] = arr[j];
            k++;
            j++;
        }
    }
    while (i <= mid)
    {
        temp[k] = arr[i];
        k++;
        i++;
    }
    while (j <= high)
    {
        temp[k] = arr[j];
        k++;
        j++;
    }
    for (int i = low; i <= high; i++)
    {
        arr[i] = temp[i - low];
    }
}

void mergesort(int *arr, int low, int high)
{
    if (low <high)
    {
        int mid = (low + high) / 2;
        mergesort(arr, low, mid);
        mergesort(arr, mid + 1, high);
        merge(arr, low, high, mid);
    }
}

I am able to implement this code in Cshell (C++ online application). However, when I use my visual studio, it said "temp[high-low+1] did not evaluate to a constant'. Therefore, I could not run my code. Anyone has any suggestions?

vahdet
  • 6,357
  • 9
  • 51
  • 106
dandelion
  • 13
  • 5

2 Answers2

1

In C++, arrays must have a compile-time constant size. Therefore, when you declare your array, the size has to be a constant expression. Some compilers like GCC allow for variable length arrays as a non-standard extension, but it comes at a cost and you really shouldn't use it in the first place.

Instead, you should make your array size constant, or use an std::vector.

eesiraed
  • 4,626
  • 4
  • 16
  • 34
-1

Some compilers in MSVS will not allow initialization of variable sized arrays. So instead of initializing your array like this

 temp[high - low + 1];

initialize it like this

int *temp = new int[high - low + 1];
  • 1
    Standard `c++` says that the size of an array must be a compile time constant. Non confirming compilers offer VLAs as an extension. – drescherjm Jul 05 '18 at 21:09
  • 2
    And where is the deallocation? – Zereges Jul 05 '18 at 21:09
  • why has this answer been downvoted???? It is a working solution!!!!!!!!!!!!!!!!!! – Roopesh Kumar Ramesh Jul 06 '18 at 06:52
  • Just because it works doesn't mean it's good code. You never freed the memory, which means that you have memory leaks. Try putting this in a loop and create a 100000 element array 1 million times. A 100000 element array of ints should take less than half a megabyte of memory normally. Also, you did not mention that C++ doesn't have variable length arrays and they're only a GCC extension. – eesiraed Jul 07 '18 at 17:45