简体   繁体   English

使用函数c ++在数组中查找元素

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

I am new to C++ and have just started learning functions. 我是C ++的新手,刚刚开始学习函数。 I have made a program to search an element in a 1-d array using a function search . 我编写了一个程序,使用函数search来搜索1-d数组中的元素。 But there is a logical error I can't comprehend! 但是有一个我无法理解的逻辑错误! Is it because of the way the function is declared ? 是因为函数的声明方式吗?

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

Output: 输出:

Enter size(<5)

4

Enter the elements of the array

4

3

2

1

Enter element to be searched

4

Element not found!

Your function always returns in the first loop iteration. 您的函数总是在第一次循环迭代中返回。 If the first element is not the one to be searched, 0 is returned immediately. 如果第一个元素不是要搜索的元素,则立即返回0。 The loop never enters the second iteration. 循环永远不会进入第二次迭代。

you must return not found if you dont found any thing, with this code, you will always return zero, if the first element is not what you are searching for. 如果您找不到任何东西,则必须返回未找到。如果此代码不是您要搜索的内容,则使用此代码,您将始终返回零。 something like this : 像这样的东西:

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

It is in your logic 这是你的逻辑

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

Let's step through that. 让我们逐步解决。 I'm going to give it [1, 2, 3, 4] , 4 , and 3 . 我要给它[1, 2, 3, 4]43

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

So, you check the first one, and if that doesn't work, it will immediately fail. 因此,您检查第一个,如果不起作用,它将立即失败。

So try this: 所以试试这个:

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

However, the better way would be to do something like this, and get rid of your global variable 但是,更好的方法是这样做,并摆脱全局变量

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

Then if you get something != -1 you have found it. 然后,如果您得到!= -1您已经找到了。

Your search function is not doing what you think it is doing: it will return 0 as soon as a[i]!=num , thus not considering the rest of the elements of the array. 您的search功能没有按照您想的去做: a[i]!=num会立即返回0 ,因此不考虑数组的其余元素。

You'd better use someting like this, with a (non-global) variable returned: 您最好使用这样的方法,并返回一个(非全局)变量:

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

The problem is with your else statement. 问题出在您的else语句上。 If the the element is not found straight away, it will automatically return 0. Furthermore, you use the integer 0 to indicate that the element is not found, but what if the element is found at position 0 (ie it is the first element of the array)? 如果没有立即找到该元素,它将自动返回0。此外,您使用整数0表示未找到该元素,但是如果在位置0处找到该元素(即它是的第一个元素),该怎么办。数组)? Then you will still say that the element is not found, even though it clearly it exists in the array. 然后,您仍然会说找不到元素,即使它显然存在于数组中也是如此。 Here is how I would do it. 这就是我要怎么做。

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

I hope you have learned about booleans (ie true or false.) Since your function's main purpose is to search, it should return whether the element is found (true) or whether it is not found (false). 我希望您了解布尔值(即true或false)。由于函数的主要目的是搜索,因此它应该返回是否找到了元素(true)或是否未找到元素(false)。 Therefore, we loop through the array, and if we find it, we output the position of the element, and return true. 因此,我们遍历数组,如果找到它,我们将输出元素的位置并返回true。 Otherwise, if we exit the loop, this means the element has not been found, so we output that and return false. 否则,如果退出循环,则意味着未找到该元素,因此我们将其输出并返回false。 This gets rid of the global variable usage and the previous problems that I have mentioned. 这摆脱了全局变量的使用以及前面提到的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM