简体   繁体   中英

finding elements in an array using a function c++

I am new to C++ and have just started learning functions. I have made a program to search an element in a 1-d array using a function search . 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. 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 .

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.

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.

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. 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)? 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). Therefore, we loop through the array, and if we find it, we output the position of the element, and return true. Otherwise, if we exit the loop, this means the element has not been found, so we output that and return false. This gets rid of the global variable usage and the previous problems that I have mentioned.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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