// Mr. Minich
// CMPSC 101
// Ch. 10 Demo Program #6
// January 30, 2000
// Purpose - a sequential (aka linear) search
#include <iostream>
using namespace std;
int main()
{
int numbers[10] = {23, 45, 2, 98, 35, 61, 9, 88, 29, 31};
// numbers to search through
int searchNum = 0; // number to search for
int index = 0; // position within array
cout << "Enter the number for which you want to search: ";
cin >> searchNum;
while((index < 10) && (searchNum != numbers[index]))
{
// Loop while the number is not found & while more elements remain.
if(numbers[index] != searchNum)
{ // If current element is not the one for which we are
index++; // searching, increment subscript index.
}
}
if(index < 10) // If loop ended before index reached 100, then the
{ // number was found.
cout << "A sequential search found the number in " << index + 1 << " comparisons." << endl;
}
else
{
cout << "Number was not found by sequential search after " << index << " comparisons." << endl;
}
return 0;
}// end of main