如何解决为什么此代码在 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 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,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 high)
{
if (low < high)
{
int pi = partition(arr,low,high);
// Separately sort elements before
// partition and after partition
quickSort(arr,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,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,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;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。