I am attempting to count the iterations in a linear search
#include <iostream>
using namespace std;
int cnt = 0;
int searchList(int stdList[], int numElems, int value)
{
int index = 0;
int position = -1;
bool found = false;
while (index < numElems && !found)
{
cnt++;
if (stdList[index] == value)
{
found = true;
position = index;
}
index++;
}
cout << cnt << endl;
return position;
}
int main() {
int array[] = { 2, 4, 0, 1, 9 };
int x = 1;
int n = sizeof(array) / sizeof(array[0]);
int result = searchList(array, n, x);
(result == -1) ? cout << "Element not found" : cout << "Element found at index: " << result;
}