#include #include #include #include using namespace std; const MaxListSize = 1000; int main() { int List[MaxListSize]; int ListSize = 0; // extract the list ifstream myin("search.dat"); for (int i = 0; (i < MaxListSize) && (myin >> List[i]); ++i) { ++ListSize; } // get the key cout << "Enter key value: "; int key; cin >> key; // search for key in values. check if index out of bounds or if we have // found value int comparisons = 0; // number of comparisons so far int i; for (i=0; (i < ListSize) && (List[i] != key); ++i) { ++comparisons; // made another check } ++comparisons; // count the check that ended the loop // display search results cout << endl; if ((i < ListSize) && (List[i] == key)) { cout << "Found " << key << ".\n" << endl << "Number of comparisons required: " << comparisons << " to process list of size " << ListSize << endl; } else { cout << "Did not find " << key << ".\n" << endl << "Number of comparisons required: " << comparisons << " to process list of size " << ListSize << endl; } // we are all done getch(); return 0; }