gpt4 book ai didi

c++ - Visual Studio 错误。它在 CodeBlocks 中工作...要求 const

转载 作者:行者123 更新时间:2023-11-30 05:39:28 25 4
gpt4 key购买 nike

#include <iostream>     // used for input and output.
#include <conio.h> // used for getch().
#include <cstdlib> // used for random number generation.
#include <time.h> // used to seed the random number generator.
#include <cmath> // used for floor

using namespace std;


void array_merger(int* array, int beg, int endd)
{
int median = floor((beg + endd) / 2); // midpoint
int min_pos = 0; // minimum index of the array
int new_beg = beg; // new beginning of the array index used to cycle through the array from left to right
int new_end = median + 1; // indexed at the midpoint of the array

// Temp array
int temp[endd-beg+1]; //declares and initializes a new temporary array to hold the unsorted data


while ( new_beg <= median && new_end <= endd ) // merges in sorted form the 2 arrays
if ( array[new_beg] < array[new_end] )
temp[min_pos++] = array[new_beg++];
else
temp[min_pos++] = array[new_end++];


while ( new_beg <= median ) // merges the remaining elements in left array
temp[min_pos++] = array[new_beg++];


while ( new_end <= endd ) // merges the remaining elements in right array
temp[min_pos++] = array[new_end++];


for ( int i = beg; i <= endd; i++ ) // moves from temp array to master array
array[i] = temp[i-beg];
}


void merge_sort(int* array, int beg, int endd)
{
if ( beg < endd ) // checks to see if there is more than one element in the array
{
int median = floor((beg + endd) / 2); // finds the midpoint of the array of 2 or more elements
merge_sort(array, beg, median); // uses recursion to further split the array in half (divide and conquer) on the left side
merge_sort(array, median + 1, endd); // now working back up from left to right
array_merger(array, beg, endd); // calls array_merger to put the pieces back together
}
}

// printer function
void print(int *array, int ARRAY_LENGTH) //prints results
{
for ( int i = 0; i < ARRAY_LENGTH; i++ ) // cycles through printing each element array from index 0 to the last index of the array
cout << array[i] << " ";
cout << endl;
}

//reusing your randomizeArray function
void randomizeArray(int array[], int length)
{
srand ( (unsigned) time(NULL)); // seed random number generator.

int i = 0;
do
{
array[i] = rand() % 100000 + 1; // a random number in the range of 1 to 100,000 is assigned.
i++;
} while (i < length);
}


void timeSort(int array[], int length)
{
clock_t startTime, endTime;
// Randomize values in array.
randomizeArray(array, length);

// Time array sort.
startTime = clock(); // Get starting time.
merge_sort(array, 0, length); // Sort array.
endTime = clock(); // Get ending time.

// Display algorithm's running time as difference between starting and ending time.
cout << "Merge sort time for an array of "
<< length
<< ": "
<< ( (float) endTime - (float) startTime) / CLOCKS_PER_SEC * 1000
<< " milliseconds." // On my machine, CLOCKS_PER_SEC is equal to 1000 and
// thus milliseconds is the correct unit.
<< endl;
}


int main()
{
//const int ARRAY_LENGTH = 20000; //sets the array length for timeSort (bigO)

//int test1[ARRAY_LENGTH]; // initializing an array for the first test
//timeSort(test1, ARRAY_LENGTH); // calling timeSort

//int test2[ARRAY_LENGTH*2];
//timeSort(test2, ARRAY_LENGTH*2);

//int test3[ARRAY_LENGTH*3];
//timeSort(test3, ARRAY_LENGTH*3);

//int test4[ARRAY_LENGTH*4];
//timeSort(test4, ARRAY_LENGTH*4);


const int ARRAY_LENGTH = 20; // tests to see if the logic is correct
int logic_test[ARRAY_LENGTH];
randomizeArray(logic_test, ARRAY_LENGTH);
cout << "Input: ";
print(logic_test, ARRAY_LENGTH); //uses print function to print unsorted array
merge_sort(logic_test, 0, ARRAY_LENGTH-1);
cout << "Output: ";
print(logic_test, ARRAY_LENGTH); //uses print function to print merge sorted array


getch();
return 0;
}

好的.. 所以我得到的错误是在 array_merger 函数中。它在初始化 int temp[endd-beg+1] 时要求常量。这导致它无法执行该方法的其余部分。

此外, int median = floor((beg+endd)/2) 也要求常量。

这些东西不会在代码块中引起问题,但在 Visual Studio 中会引起问题。

最佳答案

造成这种情况的原因肯定是编译器的不同。您的 CodeBlocks 很可能正在使用 C++ 编译器的扩展,并允许可变大小的数组。 VisualStudio 正在运行其严格的 C++ 编译器,并且在 C++ 中不允许使用可变大小的数组,并且应始终具有在编译时已知的固定大小。

根据您想要实现的目标,解决方案会有所不同。

关于c++ - Visual Studio 错误。它在 CodeBlocks 中工作...要求 const,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32340808/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com