Comparisons in an Array

Need to write a program that finds values in an array. Having a bit of trouble figuring out how to count the number of comparisons it makes until it finds the value.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <stdlib.h>
using namespace std;

int linearSearch (int array[], int size, int searchValue)
{
    for (int i = 0; i < size; i++)
    {
        if  (searchValue == array[i])
        {
            return i;
        }
    }
    
    return -1;
}

int main ()
{
    int a[] = {17, 22, 8, 44, 86, 15};
    
    int userValue;
    
    cout << "Enter an integer:" << endl;
    cin >> userValue;
    
    int result = linearSearch(a, 6, userValue);
    
    if(result >= 0)
    {
        cout << "The number " << a[result] << "was found at the element with index" << result << endl;
    }
    else
    {
        cout << "The number" << userValue << " was not found." << endl;
    }
}
The program seems to work fine.

If you mean how many times you need to compare the search value with an array element, that is the return value + 1, or std::size(a) if the value is not found.
Last edited on
Would you say that your program is now similar to:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>     // std::cout
#include <algorithm>    // std::find

int main () {
  int myints[] = { 10, 20, 30, 40 };

  int * p = std::find( myints, myints+4, 30 );

  if (p != myints+4)
    std::cout << "Found in myints at: " << std::distance( myints, p ) << '\n';
  else
    std::cout << "Not found in myints\n";
}


Lets make it more complex:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>     // std::cout
#include <algorithm>    // std::find_if

int main () {
  int myints[] = { 10, 20, 30, 40 };

  int seek = 30;
  int * p = std::find_if( myints, myints+4, [seek](int x){ return seek == x; } );

  if (p != myints+4)
    std::cout << "Found in myints at: " << std::distance( myints, p ) << '\n';
  else
    std::cout << "Not found in myints\n";
}

Yes, the lambda predicate is a bit elaborate way to write
searchValue == array[i]
particularly when the plain std::find uses exactly same relation.

However, the approach can be expanded:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <algorithm>

int main () {
  int myints[] = { 10, 20, 30, 40 };

  int seek = 30;
  size_t cnt {};
  int * p = std::find_if( myints, myints+4,
                [seek,&cnt](int x){ ++cnt; return seek == x; } );

  if (p != myints+4)
    std::cout << "Found in myints at: " << std::distance( myints, p ) << '\n';
  else
    std::cout << "Not found in myints\n";
  std::cout << "Cond " << cnt << '\n';
}

We have a counter and we increment the counter every time we are about to test the searchValue == array[i].

More specifically, the caller has a counter and the comparator has a reference to the counter. A function can take a by reference parameter ...
Last edited on
Topic archived. No new replies allowed.