// sequential search for a specific number #include #include // rand & srand #include // time using namespace std; const int LENGTH = 5; // number of random integers int main() { int numbers[LENGTH]; // random integers int i = 0; // loop variable int key = 0; // number being searched for bool found = false; // flag variable for finding the key value srand(1); // or srand(time(NULL)); for (i = 0; i < LENGTH; i++) { numbers[i] = rand() % 50 + 1; // generating random integer between 1 and 50 cout << numbers[i] << " "; } cout << "\n\nEnter a number that you'd like to search for: "; cin >> key; for (i = 0; i < LENGTH; i++) // linear search for inputted key value { if (numbers[i] == key) { cout << "Found it in position " << i << endl; found = true; break; // exit the loop so we don't needlessly inspect more values } } if (!found) // or if (found == false) { cout << "You didn't find it " << endl; } system("pause"); return 0; }// end of main