"期望的常数表达"错误

时间:2016-05-08 18:43:36

标签: visual-c++

我一直在努力制作一个合并排序代码,但这个错误信息一直显示出来。它说"期望的常量表达"即使主要功能中的大小是恒定的。我试图解决它,但我无法解决。我该怎么办?

#include<iostream>
using namespace std;
void merge(int data[], int size, int first, int mid, int last);

void mergeSort(int data[], int size, int first /*low*/, int last/*high*/)
{
    if(first<last)
    {
        //sort each half
        int mid = (first + last)/2;
        //index of midpoint 
        //sort left half the array [first .... mid]
        mergeSort(data, size, first, mid);
        //sort right half of the array [mid+1 ..... last]
        mergeSort(data, size, mid+1, last);
        //merge the two halves 
        merge(data, size, first, mid, last);
    }
}

void merge(int data[], int size, int first, int mid, int last)
{

    int tempArr[size];
    int i=first;
    int j=mid+1;
    int k=first;

    while (i<=mid && j<=last)
    {
        if (tempArr[i] <= tempArr[j])
        {
            data[k]=tempArr[i];
        }
        else
        {
            data[k] = tempArr[j];
            ++j;
        }
        ++k;
    }

    while (i <= mid)
    {
        data[k] = tempArr[i];
        ++k;
        ++i;
    }
    }

int main()
{
    const int size = 10;
    int arr[size] = {1, 0, 6, 15, 30, 56, 23, 3, 7, 10};
    mergeSort(arr, size, 0, size);
    for(int i=0; i<size; i++)
    {
        cout<< arr[i]<<" ";
    }
    system("pause");
    return 0;
}

1 个答案:

答案 0 :(得分:0)

您的代码中有几处错误。我为你重写了代码:)所以你可以更多地研究它,看看你的错误在哪里。尽管如此,仍有许多改进的空间!您可以运行代码here

#include<iostream>
using namespace std;
void merge(int data[], int size, int first, int mid, int last);

void mergeSort(int data[], int size, int first /*low*/, int last/*high*/)
{
    if(first<last-1) // do it if there are more than one lement
    {
        //sort each half
        int mid = (first + last)/2;
        //index of midpoint 
        //sort left half the array [first .... mid-1]
        mergeSort(data, size, first, mid);
        //sort right half of the array [mid ..... last-1]
        mergeSort(data, size, mid, last);
        //merge the two halves 
        merge(data, size, first, mid, last);
    }
}

void merge(int data[], int size, int first, int mid, int last)
{
    int* tempArr = new int[size];

    for(int i = 0; i < size; ++i)
        tempArr[i] = data[i];

    int i=first;
    int j=mid;
    int k=first;

    while (i<mid && j<last)
    {
        if (tempArr[i] <= tempArr[j])
        {
            data[k]=tempArr[i];
            i++;
        }
        else
        {
            data[k] = tempArr[j];
            ++j;
        }
        ++k;
    }

    while (i < mid)
    {
        data[k] = tempArr[i];
        ++k;
        ++i;
    }
    while (j < last)
    {
        data[k] = tempArr[j];
        ++k;
        ++j;
    }
    delete[] tempArr;
}

int main()
{
    const int size = 10;
    int arr[size] = {1, 0, 6, 15, 30, 56, 23, 3, 7, 10};
    mergeSort(arr, size, 0, size);
    for(int i=0; i<size; i++)
    {
        cout<< arr[i]<<" ";
    }
    return 0;
}