fork(2) download
  1. //Tahmidur Rahman CSC5 ch.8 pg. 487 #2
  2.  
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. /******************************************************************************
  7.  *
  8.  * Lottery Winners
  9.  * ____________________________________________________________________________
  10.  * A lottery ticket buyer purchases 10 tickets a week, always playing the same
  11.  * 10 5-digit "lucky" combinations. Write a program that initializes an array
  12.  * or a vector with these numbers and then lets the player enter tis week's
  13.  * winning 5 digit number. The program should perform a linear search through
  14.  * the list of the player's numbers and report whether or not one of the
  15.  * tickets is a winner this week. Here are the numbers:
  16.  *
  17.  * 13579 26791 2679, 33445 55555
  18.  * 62483 77777 79422 85647 93121
  19.  *_____________________________________________________________________________
  20.  * input
  21.  * winNums[] : array that stores all winning numbers
  22.  * lottery : stores value of the position
  23.  * entnum : enter lottery number
  24.  *
  25.  * Output
  26.  * "Sorry better luck next time" : if # entered wasn't lotter winner
  27.  * ""You're a winner!!!" : if # entered wasn the lotter winner
  28.  *
  29.  ******************************************************************************/
  30.  
  31. /*******************************************************************************
  32.  *
  33.  * Void Search
  34.  * ____________________________________________________________________________
  35.  * A simple linear search should be used to locate the number entered by the
  36.  * user
  37.  * ____________________________________________________________________________
  38.  *
  39.  * const int array[] //Array that the stores all the ID numbers
  40.  * int SIZE //global constant for size of array
  41.  * int value //user entered ID #
  42.  * int ind = 0 //subsript to search array
  43.  * int position = -1 //record position of search value
  44.  * bool found = false //flag to indiacate if value was found
  45.  *
  46.  ******************************************************************************/
  47.  
  48.  
  49. int Search( const int[], int, int);
  50. const int size = 10;
  51.  
  52. int main() {
  53.  
  54. //list of valid account numbers
  55. int winNums[size] = {13579, 26791, 26792, 33445, 55555,
  56. 62483, 77777, 79422, 85647, 93121};
  57.  
  58. int lottery;
  59. int entnum;
  60.  
  61. cout << "Enter the charge account number:\n";
  62. cin >> entnum;
  63.  
  64. //search for the winning number
  65. lottery = Search(winNums, size, entnum);
  66.  
  67. //if position returns -1
  68. if (lottery == -1)
  69. cout << "Sorry better luck next time.";
  70.  
  71. //if position returns anything but -1
  72. else
  73. {
  74. cout << "You're a winner!!!\n\n";
  75.  
  76.  
  77. }
  78.  
  79. return 0;
  80. }
  81.  
  82. int Search( const int array[], int SIZE, int value)
  83. {
  84. int ind = 0; //subsript to search array
  85. int position = -1; //record position of search value
  86. bool found = false; //flag to indiacate if value was found
  87.  
  88. while(ind < SIZE && !found)
  89. {
  90. if (array[ind] == value) //if value is found
  91. {
  92. found = true; //set flag
  93. position = ind; //record values subscript
  94. }
  95. ind++; //repeat process with next element
  96. }
  97. return position; //return position or -1
  98. }
  99.  
Success #stdin #stdout 0s 3100KB
stdin
13579
stdout
Enter the charge account number:
You're a winner!!!