fork download
  1. // Attached: HW_2d
  2. // ===========================================================
  3. // File: HW_2d
  4. // ===========================================================
  5. // Programmer: Elaine Torrez
  6. // Class: CMPR 121
  7. // ===========================================================
  8.  
  9. #include <iostream>
  10. using namespace std;
  11.  
  12. // Function prototype (optional but clean)
  13. int sequentialSearch(const int arr[], int size, int target);
  14.  
  15. int main()
  16. {
  17. const int SIZE = 5;
  18.  
  19. int idNumbers[SIZE] = {12345, 54321, 11223, 33211, 44411};
  20. int userID;
  21.  
  22. cout << "Enter an ID number to search: ";
  23. cin >> userID;
  24.  
  25. int index = sequentialSearch(idNumbers, SIZE, userID);
  26.  
  27. if (index != -1)
  28. cout << "ID found at index " << index << endl;
  29. else
  30. cout << "That ID is not in the list." << endl;
  31.  
  32. return 0;
  33. }
  34.  
  35. // ===========================================================
  36.  
  37. int sequentialSearch(const int arr[], int size, int target)
  38. {
  39. for (int i = 0; i < size; i++)
  40. {
  41. if (arr[i] == target)
  42. return i; // found
  43. }
  44. return -1; // not found
  45. }
  46.  
Success #stdin #stdout 0s 5316KB
stdin
11223
stdout
Enter an ID number to search: ID found at index 2