fork download
  1. // Marvyn Jesse De La Torre Chapter 8 #2 pg.487
  2. //
  3. /*************************************************************
  4. *
  5. * Determine Lottery Winners
  6. *
  7. * This program accepts a list of winning lottery numbers
  8. * to determine whether the user's number is a winner.
  9. *
  10. * Input:
  11. * win: The lottery number entered by the user
  12. *
  13. * Output:
  14. * Displays whether the number is a winning number.
  15. *************************************************************/
  16.  
  17. #include <iostream>
  18. using namespace std;
  19.  
  20. int linear(int arr[], int size, int win);
  21.  
  22. int main()
  23. {
  24. int arr[] = {
  25. 13579, 26791, 26792, 33445, 55555,
  26. 62483, 77777, 79422, 85647, 93121
  27. };
  28.  
  29. int size = sizeof(arr) / sizeof(arr[0]);
  30. int win;
  31.  
  32. cout << "Enter the winning number: ";
  33. cin >> win;
  34.  
  35. int position = linear(arr, size, win);
  36.  
  37. if (position == -1)
  38. {
  39. cout << "None of them are winning." << endl;
  40. }
  41. else
  42. {
  43. cout << "Congratulations! You found a winning number." << endl;
  44. }
  45.  
  46. return 0;
  47. }
  48.  
  49. int linear(int arr[], int size, int win)
  50. {
  51. int position = -1;
  52. int index = 0;
  53.  
  54. while (position == -1 && index < size)
  55. {
  56. if (arr[index] == win)
  57. {
  58. position = index;
  59. }
  60.  
  61. index++;
  62. }
  63.  
  64. return position;
  65. }
Success #stdin #stdout 0s 5340KB
stdin
26791
stdout
Enter the winning number: Congratulations! You found a winning number.