fork download
  1. // Andres Guzman CSC5 Chapter 8, P. 487, #2
  2. /******************************************************************************
  3.  *
  4.  * DETERMINE LOTTERY STATUS
  5.  * ____________________________________________________________________________
  6.  * This program will determine whether the inputed numbers for a lottery ticket
  7.  * match those of the winning numbers.
  8.  *
  9.  * Computation:
  10.  *
  11.  * Uses a linear search in the searchlist function to compare the values in the
  12.  * subscripts to the inputed values of the user. If equal, returns a positive
  13.  * status, and a negative status otherwise.
  14.  * ____________________________________________________________________________
  15.  *
  16.  * INPUT
  17.  * SIZE = Array Size
  18.  * lotterywins = The winning numbers for the lottery ticket
  19.  * lotterynum = Holds the inputed value for the lottery ticket number
  20.  *
  21.  * OUTPUT
  22.  * status = Displays the status of the lottery ticket
  23.  *
  24.  ******************************************************************************/
  25. #include <iostream>
  26. using namespace std;
  27.  
  28. int searchlist(const int a[], int b, int c); //Function Prototype
  29. const int SIZE = 10; //Array Size
  30.  
  31. int main()
  32. {
  33. //
  34. //Initialization List
  35. int lotterywins[SIZE] = {13579, 26791, 26792, 33445, 55555, 62483, 77777,
  36. 79422, 85647, 93121};
  37. int lotterynum; //Holds Input Value for users Lottery Numbers
  38. int status; //Holds Status of the lottery Ticket
  39. //
  40. //Prompt Input Display
  41. cout << "Please enter this weeks winning lottery number: ";
  42. cin >> lotterynum;
  43. //
  44. //Input Validation
  45. while(lotterynum < 0)
  46. {
  47. cout << "Cannot Be Negatives. Please Try Again." << endl;
  48. cout << "Please enter this weeks winning lottery number: ";
  49. cin >> lotterynum;
  50. }
  51. //
  52. //Adds Function Results to Variable Status
  53. status = searchlist(lotterywins, SIZE, lotterynum);
  54. //
  55. //Determines whether the inputed values are equal to the subscript
  56. if(status == -1)
  57. cout << "\nTicket is not a winner this week." << endl;
  58. else
  59. cout << "\nTicket is a winner this week!" << endl;
  60. return 0;
  61. }
  62. /*******************************************************************************
  63.  * Adding Definition to the function. This function will search through the list
  64.  * of values in the array subscript and determine whether the value inside
  65.  * matches the inputed value of the user, in which case will then determine the
  66.  * status of the lottery ticket by using a linear search.
  67. ******************************************************************************/
  68. int searchlist(const int a[], int b, int c)
  69. {
  70. int index = 0;
  71. int position = -1;
  72. bool found = false;
  73.  
  74. while (index < b && !found)
  75. {
  76. if(a[index] == c)
  77. {
  78. found = true;
  79. position = index;
  80. }
  81. index++;
  82. }
  83. return position;
  84. }
Success #stdin #stdout 0s 5360KB
stdin
11111
stdout
Please enter this weeks winning lottery number: 
Ticket is not a winner this week.