【问题标题】:Why is my array not passing to the fuction I created properly? C++为什么我的数组没有传递给我正确创建的函数? C++
【发布时间】: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);
}

【问题讨论】:

标签: c++ arrays sorting parameter-passing


【解决方案1】:

您有可能未定义的行为i = 0; i &lt; 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;
}

【讨论】:

  • 不应该isOrdered 返回bool 吗?
  • 我想是这样,但是 OP 有 int1-1 作为回报——这是一种有效的,虽然不常见的方式。 (它会使返回更简单,只需return i == n;
【解决方案2】:

无论整个数组的内容如何,​​您的函数在第一次迭代时都是 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;
}

【讨论】:

    【解决方案3】:
    1. 您确实创建了一个排序数组 [1..30](您只是在填充它时从末尾开始) - 要创建 [30..1] 您需要 c[i] = n - i; 并且您不需要循环然后倒退。

    2. 您只检查第一个元素,然后立即从函数返回,因此 [5,6,1,2] 也将被“排序”,因为您只查看 5 和 6。您应该返回“排序” " 仅当您完成整个循环而没有遇到“未排序”条件时,所以将您的“排序”结论放在循环之外

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

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

    【讨论】:

      猜你喜欢
      • 2020-01-16
      • 1970-01-01
      • 2020-06-12
      • 1970-01-01
      • 2018-10-28
      • 1970-01-01
      • 1970-01-01
      • 2015-03-21
      • 1970-01-01
      相关资源
      最近更新 更多