fork download
  1. // Kurt Feiereisel CSC5 Chapter 8, p.487, #3
  2. /*******************************************************************************
  3.  *
  4.  * Determine if a User Won or Not
  5.  * _____________________________________________________________________________
  6.  * This program allows a user to enter this weeks winning lottery numbers. The
  7.  * function will then determine and report if the user won the lottery or not.
  8.  * _____________________________________________________________________________
  9.  * INPUT:
  10.  * winning : The winning lottery numbers for the week
  11.  * OUTPUT:
  12.  * Whether the user won the lottery or not
  13.  *
  14.  * ****************************************************************************/
  15. #include <iostream>
  16. using namespace std;
  17.  
  18. // Function Prototypes
  19. int inputWinningNum(int winning);
  20. void sort(int puchased[], int SIZE);
  21. void determine(int purchased[], int winning, int SIZE);
  22.  
  23. int main()
  24. {
  25. // Initalize Variables/Constants/Arrays
  26. int winning = 0;
  27. const int SIZE = 10;
  28. int purchased[SIZE] = {13579, 26791, 26792, 33445, 55555, 62483, 77777,
  29. 79422, 85647, 93121};
  30.  
  31. // Function Call
  32. sort(purchased, SIZE);
  33. winning = inputWinningNum(winning);
  34. determine(purchased, winning, SIZE);
  35. return 0;
  36. }
  37.  
  38. /*
  39.  * Definition of inputWinningNum:
  40.  * This function allows a user to enter the winning lottery numbers. This
  41.  * function will return the value entered to main.
  42.  */
  43. int inputWinningNum(int num)
  44. {
  45. // Input num
  46. cout << "Please enter the winning lottery numbers: " << endl;
  47. cin >> num;
  48. return num;
  49. }
  50.  
  51. /*
  52.  * Definition of sort Function:
  53.  * This function will sort the array purchased into to ascending order.
  54.  */
  55. void sort(int p[], int size)
  56. {
  57. // Declare Local Variables
  58. int scan;
  59. int minIndex;
  60. int minNum;
  61.  
  62. // Implement For loop to sort Variables (selection sort)
  63. for(scan = 0; scan < (size - 1); scan++)
  64. {
  65. // Initalize Local Variables
  66. minIndex = scan;
  67. minNum = p[scan];
  68.  
  69. // For loop to sort elements of array
  70. for(int index = scan + 1; index < size; index++)
  71. {
  72. if(p[index] < minNum)
  73. {
  74. minNum = p[index];
  75. minIndex = index;
  76. }
  77. }
  78. p[minIndex] = p[scan];
  79. p[scan] = minNum;
  80. }
  81. }
  82.  
  83. /*
  84.  * Definition of determine Function:
  85.  * This function determines and reports whether the user won the lottery
  86.  */
  87. void determine(int p[], int win, int size)
  88. {
  89. int first = 0; // First array element
  90. int last = size - 1; // Last array element
  91. int middle; // Midpoint of search
  92. int position = -1; // Position of search Value
  93. bool found = false; // Flag
  94.  
  95. // Implement while loop to find winning number
  96. while (!found && first <= last)
  97. {
  98. middle = (first + last) / 2;
  99. if(p[middle] == win)
  100. {
  101. found = true;
  102. position = middle;
  103. }
  104. // Change First and last elements
  105. else if(p[middle] > win)
  106. last = middle - 1;
  107. else
  108. first = middle + 1;
  109. }
  110. if(position == -1)
  111. cout << "You lost!" << endl;
  112. else
  113. cout << "You won the lottery! The winning number was "
  114. << p[position] << ".\n";
  115. }
  116.  
  117.  
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
Standard output is empty