Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void findLinear(int arr[], int size, int query)
- {
- // declare some basic variables
- bool found = false;
- bool stop = false;
- int position = -1;
- int tries = 0;
- // Core algorithm for linear search
- while (!found && !stop)
- {
- for (int iter = 0; iter < SIZE && (found == false); iter++)
- {
- tries++;
- if (arr[iter] == query) // If the number was found
- {
- position = arr[iter];
- found = true;
- }
- else
- {
- stop = true; // This will escape the while loop.
- }
- }
- }
- if (found == true)
- {
- cout << query << " is in the array.\n";
- }
- else if (found == false)
- {
- cout << query << " was not found in the array.\n";
- }
- cout << "It took " << tries << " tries to make this determination.\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement