Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.74 KB | None | 0 0
  1. void findLinear(int arr[], int size, int query)
  2. {
  3.     // declare some basic variables
  4.     bool found = false;
  5.     bool stop = false;
  6.     int position = -1;
  7.     int tries = 0;
  8.  
  9.     // Core algorithm for linear search
  10.     while (!found && !stop)
  11.     {
  12.         for (int iter = 0; iter < SIZE && (found == false); iter++)
  13.         {
  14.             tries++;
  15.             if (arr[iter] == query)         // If the number was found
  16.             {
  17.                 position = arr[iter];
  18.                 found = true;
  19.             }
  20.             else
  21.             {
  22.                 stop = true;                // This will escape the while loop.
  23.             }
  24.         }
  25.     }
  26.  
  27.     if (found == true)
  28.     {
  29.         cout << query << " is in the array.\n";
  30.     }
  31.     else if (found == false)
  32.     {
  33.         cout << query << " was not found in the array.\n";
  34.     }
  35.     cout << "It took " << tries << " tries to make this determination.\n";
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement