fork download
  1. //Roman Lara Delgado CSC5 Chapter 8, P.487, #4
  2. //
  3. /*******************************************************************************
  4. *
  5. * Identify Invalid Account Number
  6. *_______________________________________________________________________________
  7. * This program determines if an account number is invalid meaning it does not
  8. * exist in a set of account numbers.
  9. * ______________________________________________________________________________
  10. * INPUT
  11. * accNumber_user : Account number entered by the user.
  12. *
  13. * OUTPUT
  14. * A message that says whether the account number entered
  15. * is valid or not.
  16. * ******************************************************************************
  17. * FUNCTION
  18. * ******************************************************************************
  19. * int searchList(const int [], int, int)
  20. * ------------------------------------------------------------------------------
  21. * This function locates if a value exists in an array.
  22. *
  23. * PARAMETERS:
  24. * array[] : receives the values of an array.
  25. * size : receives the number of elements
  26. * of the array.
  27. * value : receives a value that the user
  28. * wanted to determine whether the
  29. * value exists in the array or not.
  30. * ______________________________________________________________________________
  31. * void selectionSort(int[], int)
  32. * ------------------------------------------------------------------------------
  33. * This function arranges the data in ascending order
  34. *
  35. * PARAMETERS:
  36. * array[] : Array to arrange it in ascending order.
  37. * size : The number of elements of the array.
  38. *******************************************************************************/
  39. #include <iostream>
  40. using namespace std;
  41.  
  42. void selectionSort(int[], int); //Function prototype
  43. int binarySearch(const int [], int, int); //Function prototype
  44. const int SIZE = 18; //GLOBAL - Array's size.
  45.  
  46. int main ()
  47. {
  48. //Array that stores the account numbers of the data base.
  49. int accNumber_saved[SIZE] = {5658845, 8080152, 1005231, 4520125, 4562555,
  50. 6545231, 7895122, 5552012, 3852085, 8777541,
  51. 5050552, 7576651, 8451277, 7825877, 7881200,
  52. 1302850, 1250255, 4581002};
  53. int accNumber_user; //INPUT - Account number entered by the user.
  54. int results; //Variable that determines if a number is valid or not.
  55.  
  56. //Enter the account number by the user.
  57. cout << "Enter your account number: " << endl;
  58. cin >> accNumber_user;
  59.  
  60. //Arrange the array in ascending order.
  61. selectionSort(accNumber_saved, SIZE);
  62.  
  63. //Assign a value to results by calling the function searchList
  64. results = binarySearch(accNumber_saved, SIZE, accNumber_user);
  65.  
  66. //if results is -1, then the number is invalid.
  67. if(results == -1)
  68. {
  69. cout << "The number is invalid.";
  70. }
  71. else //Otherwise is valid.
  72. {
  73. cout << "The number is valid.";
  74. }
  75. return 0;
  76. }
  77.  
  78. void selectionSort (int array[], int size)
  79. {
  80. int startScan, //First element of the array[0].
  81. minIndex, //Minimum index.
  82. minValue; //Minimun value.
  83.  
  84. for(startScan = 0; startScan < (size - 1); startScan++)
  85. {
  86. minIndex = startScan; //Assign starScan in minIndex
  87. minValue = array[startScan]; //Assign the firt element of the array.
  88. for(int index = startScan + 1; index < size; index++)
  89. {
  90. if(array[index] < minValue) //If an element is less than array[0].
  91. {
  92. minValue = array[index]; //Assign the element.
  93. minIndex = index; //Assign the subscript.
  94. }
  95. }
  96. array[minIndex] = array[startScan]; //Assign the new value at first.
  97. array[startScan] = minValue;
  98. }
  99. }
  100.  
  101. //binarySearch definition
  102. int binarySearch(const int array[], int size, int value)
  103. {
  104. int first = 0, //First array element.
  105. last = size - 1, //Last array element.
  106. middle, //Midpoint of search.
  107. position = -1; //Position of search value.
  108. bool found = false; //Flag.
  109.  
  110. while (!found && first <= last)
  111. {
  112. middle = (first + last) /2; //Calculate midpoint.
  113.  
  114. if(array[middle] == value) //If value is found at mid.
  115. {
  116. found = true;
  117. position = middle;
  118. }
  119. else if (array[middle] > value) //If value is in lower half.
  120. {
  121. last = middle - 1;
  122. }
  123. else
  124. {
  125. first = middle + 1; //If value is in upper half.
  126. }
  127. }
  128. return position;
  129. } //End binarySearch
  130.  
Success #stdin #stdout 0.01s 5304KB
stdin
7881200
stdout
Enter your account number: 
The number is valid.