fork download
  1. //OrinTill Qimid CS1A Chapter 4, P. 294, 10
  2. //
  3. /**************************************************************
  4.  * Lotery ticket
  5.  *
  6.  * ____________________________________________________________
  7.  * This program will tell the user if he won the lotery or not.
  8.  * ____________________________________________________________
  9.  * INPUT
  10.  * ticket : The user ticket
  11.  * OUTPUT
  12.  * If the user won or lost the ticket.
  13.  **************************************************************/
  14. #include <iostream>
  15. #include <iomanip>
  16. #include <cmath>
  17. using namespace std;
  18. const int SIZE=10;
  19. bool binarySearch(int arr[], int size, int target) {
  20. int left = 0;
  21. int right = size - 1;
  22. int mid;
  23.  
  24. while (left <= right) {
  25. mid = left + (right - left) / 2;
  26.  
  27. // Check if target is present at mid
  28. if (arr[mid] == target) {
  29. return true;
  30. }
  31. // If target is greater, ignore the left half
  32. else if (arr[mid] < target) {
  33. left = mid + 1;
  34. }
  35. // If target is smaller, ignore the right half
  36. else {
  37. right = mid - 1;
  38. }
  39. }
  40. return false;
  41. }
  42.  
  43. int main()
  44. {
  45. bool found;
  46. found = false;
  47. int ticket;
  48. int validNums[SIZE] = {13579, 26791, 26792, 33445, 55555,
  49. 62483, 77777, 79422, 85647, 93121};
  50. cout<<"Whats your ticket number?"<<endl;
  51. cin>>ticket;
  52. found = binarySearch(validNums, SIZE, ticket);
  53. if (found == true)
  54. {
  55. cout<<"Congrats on winning!!"<<endl;
  56. }
  57. else
  58. {
  59. cout<<"Sorry u did not win the ticket"<<endl;
  60. }
  61.  
  62.  
  63. return 0 ;
  64. }
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
Standard output is empty