簡體   English   中英

布爾遞歸函數始終返回true

[英]Boolean recursive function always returns true

我正在使用遞歸進行分配。 我似乎無法弄清楚為什么數字不在數組中時函數不會返回false。 由於某種原因,在我看來,要搜索的數字已添加到數組中。 如果有人可以告訴我我要去哪里錯了,將不勝感激。

#include "stdafx.h"
#include <iostream>

using namespace std;

bool isMember(int[], const int, int);

int main() {

    const int SIZE = 5;
    int myArr[SIZE];
    int numSearched;

    cout << "Enter 5 numbers to be searched through." << endl;

    for (int i = 0; i < SIZE; i++) {
        cout << "Enter number " << i + 1 << endl;
        cin >> myArr[i];
    }

    cout << "What number do you want to find?" << endl;
    cin >> numSearched;

    if (isMember(myArr, SIZE, numSearched)) {
        cout << "True" << endl;
    }
    else {
        cout << "False" << endl;
    }

    return 0;
}

bool isMember(int arr[], const int S, int search) {
    bool found = false;

    cout << arr[S] << endl;

    if (arr[S] == search) {

        found = true;
        return found;
    }
    else if ((arr[S] == 0) && (arr[0] != search)) {

        return found;
    }
    else {

        return isMember(arr, S - 1, search);
    }
}

許多人指出,您在嘗試訪問超出陣列大小的內存時遇到了內存訪問問題。 在函數的頂級調用中,已經引起了問題,因為您將SIZE作為數組索引參數進行了傳遞。 如果SIZE是數組的大小,則arr[SIZE-1]是數組在內存中的最后一個元素。 arr[SIZE]是最后一個元素。 訪問超出陣列內存占用空間的內存會導致未定義的行為,這很糟糕。

總體而言,不良索引在這里是一個巨大的問題。 但是,即使您解決了上面的問題,另一個問題行仍在此處,因為您試圖在S達到0時停止,但您寫錯了這一點。

else if ((arr[S] == 0) && (arr[0] != search)) {

您希望它是:

else if (S == 0) {

語句arr[0] != search是多余的,因為上面的條件已經對此進行了檢查。 原始語句arr[S] == 0試圖將S處的arr值與arr[S] == 0進行比較,而不是測試索引變量現在為0,我建議的代碼會這樣做。

但這也可能解釋了為什么盡管行為未定義,但函數始終返回true,並且程序沒有崩潰。 由於您的函數未適當終止,因此它將連續調用isMember(...,S-1,...) 因此,它將繼續減小索引並更改所訪問的arr[S]存儲位置。 該過程將一直進行到找到arr[S] == 0或找到您要查找的值為止。 碰巧的是,您遇到內存中的目標值之前遇到0。

您將索引號發送到從零開始的isMember,當您將5發送到isMember時,未定義成員arr [5]。 並且應該使用類似的方法

isMember(myArr, SIZE - 1, numSearched)

而且您的代碼沒有結束條件,我在S < 0之后將結束條件添加到您的代碼中以結束遞歸

if (S < 0)
    return false;

嘗試這個 ;)

#include <iostream>

using namespace std;

bool isMember(int[], const int, int);

int main() {

    const int SIZE = 5;
    int myArr[SIZE];
    int numSearched;

    cout << "Enter 5 numbers to be searched through." << endl;

    for (int i = 0; i < SIZE; i++) {
        cout << "Enter number " << i + 1 << endl;
        cin >> myArr[i];
    }

    cout << "What number do you want to find?" << endl;
    cin >> numSearched;

    if (isMember(myArr, SIZE - 1, numSearched)) {
        cout << "True" << endl;
    }
    else {
        cout << "False" << endl;
    }

    return 0;
}

bool isMember(int arr[], const int S, int search) {
    if (S < 0)
        return false;
    bool found = false;

    //cout << "index is " << S << "\t" <<  arr[S] << endl;

    if (arr[S] == search) {

        found = true;
        return found;
    }

    return isMember(arr, S - 1, search); 
}

錯誤是在遞歸函數的停止條件下:

else if ((arr[S] == 0) && (arr[0] != search)) {

    return found;
}

您沒有將索引檢查為第一個索引,而是將內容檢查為零。 您可以嘗試如下操作:

else if (S <= 0) {

    return false;
}

您也不需要檢查該值以匹配“搜索”,因為它與之前的條件是多余的。 您還可以直接返回false。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM