首页 > 解决方案 > 为什么此代码在 Visual Studio 中失败,但在在线编译器中却没有?

问题描述

此代码在 4 个不同的站点上运行,到目前为止,我还没有测试过更多,但不适用于 Visual Studio 程序。错误在于 n1 和 n2。错误是“表达式未计算为常量”和“数组类型'int [n1]'不可分配”。我不明白为什么错误发生在 Visual Studio 程序上,但没有在线编译器。我读到了一些关于在线编译器上提高检查错误水平的文章,但是我使用的 4 没有看到这样做的选项。我将如何解决此错误?

#include <iostream>
#include <stdio.h>
#include <list>
#include <chrono>
#include <algorithm>
using namespace std::chrono;
using namespace std;

// Part 2 to using Merge sort to sort my list
void merge(int arr[], int z, int l, int arr_size)
{
    int n1 = l - z + 1;
    int n2 = arr_size - l;
 
    int L[n1], R[n2];  //expression error occurs here for both n1 and n2
 
    for (int i = 0; i < n1; i++)
        L[i] = arr[z + i];  // array type error occurs here
    for (int j = 0; j < n2; j++)
        R[j] = arr[l + 1 + j]; // array type error occurs here

    int i = 0;
    int j = 0;
    int k = z;
 
    while (i < n1 && j < n2) {
        if (L[i] <= R[j]) {
            arr[k] = L[i];
            i++;
        }
        else {
            arr[k] = R[j];
            j++;
        }
        k++;
    }
 
    while (i < n1) {
        arr[k] = L[i];
        i++;
        k++;
    }
    
    while (j < n2) {
        arr[k] = R[j];
        j++;
        k++;
    }
}

//Function that uses Merge sort to sort my list
void mergeSort(int arr[], int z, int arr_size)
{
    int l;
        if(z < arr_size)
        {
            l = (z + arr_size)/ 2;
            mergeSort(arr, z, l);
            mergeSort(arr, z+1, arr_size);
            merge(arr, z, l, arr_size); //< not declared in scope?
        }
}

//Function to print out results of sorting using Merge sort method
void mergePrint(int arr[], int arr_size)
{
    int i; 
    cout << "Using Merge Sort method: ";
    for (i = 0; i < arr_size; i++)  
        cout << arr[i] << " "; 
}

//Part 3 to using Quick sort to sort my list
void swap(int* a, int* b) 
{ 
    int t = *a; 
    *a = *b; 
    *b = t; 
} 
 
//Part 2 to using Quick sort to sort my list
int partition (int arr[], int low, int high) 
{ 
    int pivot = arr[high]; // pivot 
    int i = (low - 1); // Index of smaller element and indicates the right position of pivot found so far
 
    for (int j = low; j <= high - 1; j++) 
    { 
        // If current element is smaller than the pivot 
        if (arr[j] < pivot) 
        { 
            i++; // increment index of smaller element 
            swap(&arr[i], &arr[j]); 
        } 
    } 
    swap(&arr[i + 1], &arr[high]); 
    return (i + 1); 
} 

//Function that uses Quick sort to sort my list
void quickSort(int arr[], int low, int high)
{
    if (low < high) 
    { 
        int pi = partition(arr, low, high); 
 
        // Separately sort elements before 
        // partition and after partition 
        quickSort(arr, low, pi - 1); 
        quickSort(arr, pi + 1, high); 
    } 
}

//Function to print out results of sorting using Quick sort method
void quickPrint(int arr[], int arr_size)
{
    int i; 
    cout << "Using Quick Sort method: ";
    for (i = 0; i < arr_size; i++)  
        cout << arr[i] << " ";  
}
/*
//Function that uses Median sort to sort my list
void medianSort()
{
    
}

//Function to print out results of sorting using Median sort method
void medianPrint()
{
    int i; 
    cout << "Using Median Sort method: ";
    for (i = 0; i < arr_size; i++)  
        cout << arr[i] << " ";  
}
*/
//Function that uses Insertion sort to sort my list
void insertionSort(int arr[], int arr_size)
{
    int i,j,k;  
        for (i = 1; i < arr_size; i++) 
        {  
            k = arr[i];  
            j = i - 1;  
                while (j >= 0 && arr[j] > k) 
                {  
                    arr[j + 1] = arr[j];  
                    j = j - 1;  
                }  
            arr[j + 1] = k;  
        }  
    }  
//Function to print out results of sorting using Insertion sort method
void insertionPrint(int arr[], int arr_size)  
{  
    int i; 
    cout << "Using Insertion Sort method: ";
    for (i = 0; i < arr_size; i++)  
        cout << arr[i] << " "; 

}

int main()
{
    // Declaring numbers
    int arr[] = {6, 10, 12, 3, 7, 1}; // tried with 8 values but Merge sort does not sort entirely
    int arr_size = sizeof(arr)/ sizeof(arr[0]);
    
    //Captures time it takes to run Merge Sort function
    auto mStart = high_resolution_clock::now();
    
        //Call Merge sort functions
        mergeSort(arr, 0, arr_size - 1);
        mergePrint(arr, arr_size);
    
    auto mStop = high_resolution_clock::now();
    auto mDuration = duration_cast<microseconds>(mStop - mStart);
    
    cout << "\nTime taken by Merge Sort function : "<< mDuration.count() << " microseconds" <<endl;
    
    
    //Captures time it takes to run Quick Sort function
    auto qStart = high_resolution_clock::now();
    
        //Call Quick sort functions
        quickSort(arr, 0, arr_size - 1);
        quickPrint(arr, arr_size);
    
    auto qStop = high_resolution_clock::now();
    auto qDuration = duration_cast<microseconds>(qStop - qStart);
    
    cout << "\nTime taken by Quick Sort function : "<< qDuration.count() << " microseconds" <<endl;
    
    //Captures time it takes to run Insertion Sort function
    auto iStart = high_resolution_clock::now();
    
        //Calls Insertion Sort functions
        insertionSort(arr, arr_size);
        insertionPrint(arr, arr_size);
    
    auto iStop = high_resolution_clock::now();
    auto iDuration = duration_cast<microseconds>(iStop - iStart);
    
   cout << "\nTime taken by Insertion Sort function : "<< iDuration.count() << " microseconds";
   
   
    return 0;
}

标签: c++arraysvisual-studiovisual-c++compiler-errors

解决方案


推荐阅读