fork download
  1. //Nathan Dominguez CSC5 Chapter 8, P.487, #2
  2. //
  3. /*******************************************************************************
  4.  *
  5.  * Determine Lottery Winner with Linear Search
  6.  * ____________________________________________________________________________
  7.  * This prorgam will prompt the user to enter this week's winning 5-digit
  8.  * numbers. The program will then use a linear search to validate the numbers
  9.  * to the list of numbers given.
  10.  *______________________________________________________________________________
  11.  * INPUT
  12.  * number : Numbers entered
  13.  * num[] : Array for numbers given
  14.  *
  15.  * Output
  16.  * result : ouput for when the user wins or not
  17.  ******************************************************************************/
  18. #include <iostream>
  19. #include <iomanip>
  20. using namespace std;
  21.  
  22. //PROTOTYPE search()
  23. int search(int num[], //Input - Numbers given array
  24. int number, //Input - Number entered
  25. const int SIZE); //Array size
  26.  
  27.  
  28. int main()
  29. {
  30. const int SIZE = 10; //Size of the array
  31. int num[SIZE] = {13579, 26791, 26792, 33445, //CONSTANT - array numbers
  32. 55555, 62483, 77777, 79422, 85647, 93121};
  33.  
  34. int number; //INPUT - number entered by the user
  35. int result; //OUTPUT - value to hold if its found or not
  36. //
  37. //INPUT - enter number
  38. cout << "Please enter this week's winning 5-digit numbers " << endl;
  39. cin >> number;
  40. //
  41. //Invoke search function
  42. result = search(num, number, SIZE);
  43. //
  44. //Output if user did not win
  45. if(result < 1)
  46. cout<<"Oh No! You did not win. Sorry!"<<endl;
  47. //
  48. //Output if user won
  49. else
  50. cout<<"Woohoo!! You Won!!"<<endl;
  51. return 0;
  52. }
  53.  
  54. int search(int num[], int number, const int SIZE)
  55. {
  56. int index = 0; //counter
  57. bool found = false; //Flag to indicate if value was found
  58. //
  59. //VALIDATE
  60. while( index < SIZE && !found)
  61. {
  62. if(num[index] == number)
  63. found = true; // Set flag
  64. index++;
  65. }
  66. return found;
  67. }
Success #stdin #stdout 0s 5292KB
stdin
13579
stdout
Please enter this week's winning 5-digit numbers 
Woohoo!! You Won!!