If length of array in sequential search is not given and there is a lot of out of order indexing,
- how many times we should execute the for loop?
- what should be the condition of for loop?
I counted indexing again and again.
#include <iostream>
using namespace std;
int main() {
int array[] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200,
300, 400, 5006, 110, 550, 440, 330, 331, 41};
// length of array is not provided
int i, n, loc = -1;
cout << "Enter value to find" << endl;
cin >> n;
for (i = 0; i < 5; i++) {
if (array[i] == n) {
loc = i;
}
}
if (loc == -1)
cout << "Number is not found in array" << endl;
else
cout << "the number is found at position " << loc << " and is " << n;
return 0;
}
For loop Condition for unknown length array in sequential search