First you might change your if condition because you already test one time your searchArray == arr[i]
if (searchArray == arr[i]) {
cout << "Found value " << searchArray << " at index " << i << ", taking " << counter << " checks !";
if (counter == 1) {
cout << "We ran into the best case scenario! " << endl;
}else if(counter == dimension) {
cout << "We ran into the worst-case scenario!" << endl;
}
}
For checking if you found the value or not, I will probably use a boolean
find = false;
for (int i = 0; i < dimension; i++) {
counter = counter + 1;
if (searchArray == arr[i]) {
cout << "Found value " << searchArray << " at index " << i << ", taking " << counter << " checks !";
find = true;
if (counter == 1) {
cout << "We ran into the best case scenario! " << endl;
}else if(counter == dimension) {
cout << "We ran into the worst-case scenario!" << endl;
}
}
}
if (!find){
cout << "The value v was not found in the array!" << endl;
}
solved Running a Search Array