fork download
  1. //Sam Partovi CS1A Chapter 8, P 487, #2
  2. //
  3. /*******************************************************************************
  4. * COMPARE LOTTERY NUMBERS
  5. * ____________________________________________________________
  6. * This program compares a given set of lottery numbers against a winning lottery
  7. * number and determines if the numbers match, in which case a win is decided.
  8. * ____________________________________________________________
  9. *INPUT
  10. * SIZE: Array size for given lottery numbers
  11. * winningNumber: Winning lottery number
  12. * lotteryTickets: Array of given lottery numbers
  13. *
  14. *OUTPUT
  15. * winFlag: Flag for matching numbers
  16. ******************************************************************************/
  17. #include <iostream>
  18. #include <iomanip>
  19. using namespace std;
  20.  
  21. int main() {
  22. const int SIZE = 10; //INPUT - Array size for given lottery numbers
  23. int winningNumber; //INPUT - Winning lottery number
  24. bool winFlag; //OUTPUT - Flag for matching numbers
  25.  
  26. //INPUT - Array of given lottery numbers
  27. int lotteryTickets[SIZE] = {13579, 26791, 26792, 33445, 55555, 62483,
  28. 77777, 79422, 85647, 93121};
  29.  
  30. //Initialize winFlag to false
  31. winFlag = false;
  32.  
  33. //Prompt for winning number to be compared
  34. cout << "Winning lottery number: ";
  35. cin >> winningNumber;
  36.  
  37. //Linear search, winFlag is true if a match is found
  38. for(int i = 0; i < SIZE; i++) {
  39. if(lotteryTickets[i] == winningNumber) {
  40. winFlag = true;
  41. }
  42. }
  43.  
  44. //Display results
  45. if(winFlag == true) cout << "\nYou won!";
  46. else cout << "\nYou were not a winner.";
  47.  
  48. return 0;
  49. }
  50.  
  51.  
Success #stdin #stdout 0s 5284KB
stdin
77777
stdout
Winning lottery number: 
You won!