簡體   English   中英

使用函數c ++在數組中查找元素

[英]finding elements in an array using a function c++

我是C ++的新手,剛剛開始學習函數。 我編寫了一個程序,使用函數search來搜索1-d數組中的元素。 但是有一個我無法理解的邏輯錯誤! 是因為函數的聲明方式嗎?

int pos;    
using namespace std;

int search(int *a, int size, int num);

int search(int *a, int size, int num)  
{      
    int i;    
    for(i=0; i<size; i++)    
    {    
        if(a[i]==num)    
        {
            pos=i; return 1;
        }
        else
            return 0;
    }
}

int main()
{  
    int a[5], size, num, i;    
    system("cls");    
    cout<<"Enter size(<5) \n";
    cin>>size;
    cout<<"Enter the elements of the array \n";
    for(i=0; i<size; i++)
        cin>>a[i];
    cout<<"Enter the number to be searched \n";
    cin>>num;
    int b = search( a, size, num);
    if(b==0)
    {
        cout<<"Element not found!"; exit(0);
    }
    else
        cout<<"Element found at position "<<(pos+1);
    system("pause");
    return 0;
}

輸出:

Enter size(<5)

4

Enter the elements of the array

4

3

2

1

Enter element to be searched

4

Element not found!

您的函數總是在第一次循環迭代中返回。 如果第一個元素不是要搜索的元素,則立即返回0。 循環永遠不會進入第二次迭代。

如果您找不到任何東西,則必須返回未找到。如果此代碼不是您要搜索的內容,則使用此代碼,您將始終返回零。 像這樣的東西:

int search(int *a, int size, int num)  
{      
    int i;    
    for(i=0; i<size; i++)    
    {    
        if(a[i]==num)    
        {
            pos=i; return 1;
        }
    }
    return 0;
}

這是你的邏輯

int search(int *a, int size, int num)  
{      
    int i;    
    for(i=0; i<size; i++)    
    {    
        if(a[i]==num)    
        {
            pos=i; return 1;
        }
        else
            return 0;
    }
}

讓我們逐步解決。 我要給它[1, 2, 3, 4]43

i => 0
a[0] => 1
a[0] == 3 => false
return false

因此,您檢查第一個,如果不起作用,它將立即失敗。

所以試試這個:

int search(int *a, int size, int num)  
{      
    for(int i = 0; i < size; ++i)    
    {    
        if(a[i]==num)    
        {
            pos = i;
            return 1;
        }
    }
    return 0;
}

但是,更好的方法是這樣做,並擺脫全局變量

int search(int *a, int size, int num)  
{      
    for(int i = 0; i < size; ++i)    
    {    
        if(a[i]==num)    
        {
            return i;
        }
    }
    return -1;
}

然后,如果您得到!= -1您已經找到了。

您的search功能沒有按照您想的去做: a[i]!=num會立即返回0 ,因此不考慮數組的其余元素。

您最好使用這樣的方法,並返回一個(非全局)變量:

#include <cstdlib>

// returns -1 if not found, else the found index 
int search(int *a, int size, int num)  
{      
    int pos = -1;
    for(int i=0; i<size; i++)
    {
        if(a[i]==num)
        { 
            pos = i;
            break;
        }
    }
    return pos;
}

// ... main() and parsing stuff goes here

if( (b = search( a, size, num)) == -1)
{
    std::cerr<<"Element not found!"; 
    return EXIT_FAILURE;
}

問題出在您的else語句上。 如果沒有立即找到該元素,它將自動返回0。此外,您使用整數0表示未找到該元素,但是如果在位置0處找到該元素(即它是的第一個元素),該怎么辦。數組)? 然后,您仍然會說找不到元素,即使它顯然存在於數組中也是如此。 這就是我要怎么做。

bool search(int *a, int size, int num)
{
    for (int i = 0; i < size; ++i)
    {
        if (a[i] == num)
        {
            cout << "Element found at position " << i << " of the array!" << endl;
            return true;
        }
    }

    cout << "Element not found!" << endl;
    return false;
}

我希望您了解布爾值(即true或false)。由於函數的主要目的是搜索,因此它應該返回是否找到了元素(true)或是否未找到元素(false)。 因此,我們遍歷數組,如果找到它,我們將輸出元素的位置並返回true。 否則,如果退出循環,則意味着未找到該元素,因此我們將其輸出並返回false。 這擺脫了全局變量的使用以及前面提到的問題。

暫無
暫無

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

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