为什么我的数组没有传递给我正确创建的函数? C++

Why is my array not passing to the fuction I created properly? C++

我需要编写一个函数来检查数组是否按从最小值到最大值的顺序通过它。我遇到的问题是该函数对所有数组都是 returning true。我在函数中放置了一个 'cout' 语句,注意到数字与数组不匹配。例如,当按下 4 时,由于数组将是 [30..1],它应该 return 数组并且它没有排序。然而实际的 return 是:

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);
}

您有可能 未定义行为i = 0; i < n 迭代并访问 arr[i+1] —— 当 i == n - 1。 “潜力”是因为您永远无法到达那里——您将在第一次迭代中返回。

相反,为什么不简单地做:

int isOrdered(int arr[], int n) {
    
    int i = 1;
    
    for (; i < n; i++)
        if (a[i-1] > a[i])
            break;
    
    return i == n ? 1 : -1;
}
  1. 您确实创建了一个排序数组 [1..30](您只是从末尾开始填充它)- 要创建 [30..1],您需要 c[i] = n - i; 然后你不需要向后循环。

  2. 你只检查第一个元素,然后你立即从函数中 return,所以 [5,6,1,2] 也会被“排序”,因为你只看在 5 和 6。只有当你完成整个循环而没有 运行 进入“未排序”条件时,你才应该 return 作为“排序”,所以把你的“排序”结论 在循环之外

  3. 由于 i < n 而不是 i < n - 1,您访问了超出数组末尾的一个元素,这是一种危险的未定义行为(并且会弄乱您的结果)。

另外,附注:使用调试器单步执行代码将极大地帮助您了解问题出在哪里,并且您不需要每次出现问题时都在这里询问。您会注意到数组实际上包含升序而不是降序数字,并且您还会注意到比较函数 returns 仅在一次检查后,而不是循环。

无论整个数组的内容如何,​​您的函数都在第 1 次迭代中 return'ing。一旦检测到不匹配就需要return false,但是在循环完成检查整个数组并且没有检测到不匹配之前不要return true,例如:

bool isOrdered(int arr[], int n) {
    if (n < 1) {
        cout << "array is empty" << endl;
        return false;
    }
    cout << arr[0];
    for (int i = 1; i < n; ++i) {
        cout << ' ' << arr[i];
        if (arr[i] < arr[i - 1]) {
            cout << "array is not sorted" << endl;
            return false;
        }
    }
    cout << "array is sorted" << endl;
    return true;
}