0

I was using an array linear search function to search in an array (the example array here is just a set of twenty int 0). But the "Value not found" statement still exists when there are values found in the main function.

#include<iostream>
#include<string>

using namespace std;

// linear search of key value in array[]
// return the index of first occurrence of key in array[]
// return -1 if key is not found in array[]
int linearSearch(const int array[], const int sizeOfArray, int key, int startPos) {
    for (int j = startPos; j < sizeOfArray; ++j)
    {
        if (array[j] == key)
            cout << j << ",";
        startPos = j + 1;
    }
    return -1;
}

int main()
{
    const int sizeOfArray = 20; // size of array
    int a[sizeOfArray]; // declare array a
    int searchKey; // value to locate in array a

    // fill in some data to array
    for (int i = 0; i < sizeOfArray; ++i)
        a[i] = 0;

    cout << "Enter an integer to search: ";
    cin >> searchKey;

    // try to locate searchKey in a
    int element = linearSearch(a, sizeOfArray, searchKey, 0);

    // display search results
    if (element = -1)
        cout << "Value not found" << endl;
    else
        cout << "Value found in element " << linearSearch(a, sizeOfArray, searchKey, 0) << endl;

    return 0;
}

And the trial execution was like:sample output

Just wondering what's wrong with it. How can I delete the "Value not found" here?

Stack Danny
  • 7,754
  • 2
  • 26
  • 55
Janet
  • 9
  • 2
  • 8
    `if ( element = -1 )` in c++ the `=` is an assignment. Remember that `==` is comparison. In this line you set element to -1 then since its not 0 you execute `cout << "Value not found" << endl;` – drescherjm Nov 23 '22 at 14:25
  • 3
    After you fix what drescherjm mentioned, your `linearSearch` function is incorrect, it always returns -1. – ChrisMM Nov 23 '22 at 14:29

1 Answers1

3

You need to fix two problems:

  1. your function doesn't return the index when it finds the element:

    int linearSearch(const int array[], const int sizeOfArray, int key, int startPos) {
        for (int j = startPos; j < sizeOfArray; ++j)
        {
            if (array[j] == key) {
                return j;
            }
        }
        return -1;
    }
    
  2. your comparison is not asking for equality, but rather setting element to -1 than that expression is true (any non-0 value is true). You have to use ==:

    if (element == -1)
        cout << "Value not found" << endl;
    

Read here why you shouldn't be using namespace std;.

Instead of C-Style arrays, prefere std::vector for dynamic length arrays and std::array for static length arrays.

Stack Danny
  • 7,754
  • 2
  • 26
  • 55
  • Thank you so much for the suggestion! But the problem is that if I use "return j" in the search function, it seems like the main function cannot print out all the occurrences but only the first one – Janet Nov 23 '22 at 14:42
  • 1
    your comment said `// return the index of first occurrence of key in array[]`. Well, if you need all occourencies, you need to `push_back` them into a `std::vector`, and if the vector is empty, no element was found, otherwise, print out the vector. – Stack Danny Nov 23 '22 at 14:44
  • 1
    It should also be stated that you should be using either [std::array](https://en.cppreference.com/w/cpp/container/array) or [std::vector](https://en.cppreference.com/w/cpp/container/vector) from the STL rather than a raw array. – Sailanarmo Nov 23 '22 at 15:32