fork download
  1. //OrinTill Qimid CS1A Chapter 4, P. 294, 10
  2. //
  3. /**************************************************************
  4.  * Check guest list
  5.  *
  6.  * ____________________________________________________________
  7.  * This program will if the user name is on the list or not.
  8.  * ____________________________________________________________
  9.  * INPUT
  10.  * name: User Name
  11.  * OUTPUT
  12.  * If the user name is on the list or not.
  13.  **************************************************************/
  14. #include <iostream>
  15. #include <iomanip>
  16. #include <cmath>
  17. using namespace std;
  18. bool binarySearch(string arr[], int size, string target)
  19. {
  20. int left;
  21. int right;
  22. left = 0;
  23. right = size-1;
  24. int mid;
  25. //checking if the number is the first or last number.
  26. if (arr[left].compare (target))
  27. {
  28. return true;
  29. }
  30. if (arr[right].compare (target))
  31. {
  32. return true;
  33. }
  34.  
  35. while (left <= right)
  36. {
  37. mid = left + (right - left) / 2;
  38.  
  39. // Check if target is present at mid
  40. if (arr[mid].compare (target))
  41. {
  42. return true;
  43. }
  44. // If target is greater, ignore the left half
  45. else if (arr[mid] < target) {
  46. left = mid + 1;
  47. }
  48. // If target is smaller, ignore the right half
  49. else {
  50. right = mid - 1;
  51. }
  52. }
  53. return false;
  54. }
  55. string* Sort(string arr[], int size)
  56. {
  57. string lowest;
  58. int var;
  59.  
  60. for (int s = 0; s < size; s++)
  61. {
  62. lowest = arr[s];
  63. var = s;
  64. for (int i = s+1; i < size; i++)
  65. {
  66. if (lowest > arr[i])
  67. {
  68.  
  69. lowest = arr[i];
  70. var = i;
  71. }
  72.  
  73. }
  74. arr[var] = arr[s];
  75. arr[s] = lowest;
  76. }
  77.  
  78. return arr;
  79.  
  80. }
  81. int main()
  82. {
  83.  
  84. const int NUM_NAMES = 20;
  85. string name;
  86. bool found;
  87. found = false;
  88. cout<<"What is your name"<<endl;
  89. cin>>name;
  90.  
  91. string names[NUM_NAMES] = {"Collins, Bill", "Smith, Bart", "Allen, Jim",
  92. "Griffin, Jim", "Stamey, Marty", "Rose, Geri",
  93. "Taylor, Terri", "Johnson, Jill",
  94. "Allison, Jeff", "Looney, Joe", "Wolfe, Bill",
  95. "James, Jean", "Weaver, Jim", "Pore, Bob",
  96. "Rutherford, Greg", "Javens, Renee",
  97. "Harrison, Rose", "Setzer, Cathy",
  98. "Pike, Gordon", "Holland, Beth"};
  99. found = binarySearch(Sort(names, NUM_NAMES), NUM_NAMES, name);
  100. if (found == true)
  101. {
  102. cout<<"Your name is on the list"<<endl;
  103. }
  104. else
  105. {
  106. cout<<"Your name is not on the list"<<endl;
  107. }
  108.  
  109.  
  110. return 0 ;
  111. }
Success #stdin #stdout 0s 5276KB
stdin
Standard input is empty
stdout
Standard output is empty