【发布时间】:2020-07-19 03:21:55
【问题描述】:
我需要编写一个函数来检查一个数组是否按照从最小值到最大值的顺序通过它。我遇到的问题是该函数对所有数组都返回 true。我在函数中放了一个“cout”语句,注意到数字与数组不匹配。例如,当按下 4 时,由于数组是 [30..1],它应该返回数组并且它是无序的。然而实际回报是:
18224
array is sorted
1
这是不正确的。我做错了什么?
int isOrdered(int arr[], int n) {
if (n <= 1) // array with 1 or no elements
return 1; // is always sorted
for (int i = 1; i < n; i++) {
cout << arr[i] << ' ';
cout << endl;
if (arr[i-1] >= arr[i]) {
cout << "array is not sorted " << endl;
return -1;
}
}
return 1;
}
int main() {
int input;
// Prompt for user
cout << "----------------------------------------------" << endl;
cout << " Press 1 to exit the program " << endl;
cout << " Press 2 to select the array that is sorted in increasing order " << endl;
cout << " Press 3 to select the array that is randomly sorted " << endl;
cout << " Press 4 to select the array that is sorted in decreasing order " << endl;
cout << "----------------------------------------------" << endl;
cin >> input;
// catch if input not within bounds
while (input != 1 && input != 2 && input != 3 && input != 4)
{
cout << "----------------------------------------------" << endl;
cout << " Press 1 to exit the program " << endl;
cout << " Press 2 to select the array that is sorted in increasing order " << endl;
cout << " Press 3 to select the array that is randomly sorted " << endl;
cout << " Press 4 to select the array that is sorted in decreasing order " << endl;
cout << "----------------------------------------------" << endl;
cin >> input;
}
while (input != 1)
{
int n = 30;
int* a = new int[n];
int* b = new int[n];
int* c = new int[n];
int* a_c = new int[n];
int* b_c = new int[n];
int* c_c = new int[n];
if (input == 2) {
for (int i = 0; i < n; i++) {
a[i] = i + 1;
}
// duplicated array needed to be created per instructions
for (int i = 0; i < n; i++) {
a_c[i] = a[i];
}
cout << isOrdered(a_c, n) << endl;
}
else if (input == 3) {
/* seed the PRNG (MT19937) using a variable value (in our case, s)*/
std::mt19937 generator(1); // seed by variable input
std::uniform_int_distribution<int> distribution(1, n); // random numbers need to be in range between 1, n
for (int i = 0; i < n; i++) {
b[i] = distribution(generator);
//cout << b[i] << ' '; // testing
}
// create duplicate
for (int i = 0; i < n; i++) {
b_c[i] = b[i];
cout << b_c[i] << ' ';
}
cout << isOrdered(b, n) << endl;
}
else {
for (int i = n-1; i >= 0; i--) {
c[i] = i + 1;
}
// create duplicate
for (int i = 0; i < n; i++) {
c_c[i] = c[i];
}
cout << isOrdered(c_c, n) << endl;
}
// Prompt user again
cout << "----------------------------------------------" << endl;
cout << " Press 1 to exit the program " << endl;
cout << " Press 2 to select the array that is sorted in increasing order " << endl;
cout << " Press 3 to select the array that is randomly sorted " << endl;
cout << " Press 4 to select the array that is sorted in decreasing order " << endl;
cout << "----------------------------------------------" << endl;
cin >> input;
// catch if input not within bounds
while (input != 1 && input != 2 && input != 3 && input != 4)
{
cout << "----------------------------------------------" << endl;
cout << " Press 1 to exit the program " << endl;
cout << " Press 2 to select the array that is sorted in increasing order " << endl;
cout << " Press 3 to select the array that is randomly sorted " << endl;
cout << " Press 4 to select the array that is sorted in decreasing order " << endl;
cout << "----------------------------------------------" << endl;
cin >> input;
}
}
exit(0);
}
【问题讨论】:
-
isOrdered包含一个循环,但它总是在该循环的第一次迭代时返回。它永远不会超过比较arr[0]和arr[1] -
阅读good C++ programming book,然后阅读this C++ reference。最好使用C++ standard containers。 另请阅读 C++ 编译器(例如 GCC...)和调试器(例如 GDB...)的文档
标签: c++ arrays sorting parameter-passing