fork download
  1. // Andres Guzman CSC5 Chapter 8, P. 487, #4
  2. /******************************************************************************
  3.  *
  4.  * DETERMINE NUMBER VALIDATION
  5.  * ____________________________________________________________________________
  6.  * This program will determine whether the inputed numbers in the account match
  7.  * those of the valid numbers.
  8.  *
  9.  * Computation:
  10.  *
  11.  * Uses a selection sort to sort the numbers from lowest to highest and then
  12.  * uses binary search in the binary function to divide the values in halves in
  13.  * the search range to the input values of the user until the values are either
  14.  * equal or invalid.
  15.  * ____________________________________________________________________________
  16.  *
  17.  * INPUT
  18.  * account = The valid numbers
  19.  * accountnum = Holds the inputed value for account numbers
  20.  *
  21.  * OUTPUT
  22.  * array = Displays the status of the inputed numbers
  23.  *
  24.  ******************************************************************************/
  25. #include <iostream>
  26. using namespace std;
  27.  
  28. int binary(const int a[], int b, int c); //Function Prototype
  29. void selectionsort(int a[], int b); //Function Prototype
  30. const int SIZE = 18;
  31. int main()
  32. {
  33. //
  34. //Initialization List
  35. int account[SIZE] = {5658845, 4520125, 7895122, 8777541, 8451277, 302850,
  36. 8080125, 4562555, 5552012, 5050552, 7825877, 1250255,
  37. 1005321, 6545231, 3852085, 7576651, 7881200, 4581002};
  38. int accountnum; //Holds Input Value for users account numbers
  39. int array; //Holds the results of the numbers
  40. //
  41. //Prompt Input Display
  42. cout << "Please enter a Number: ";
  43. cin >> accountnum;
  44. //
  45. //Input Validation
  46. while(accountnum < 0)
  47. {
  48. cout << "Cannot Be Negatives. Please Try Again." << endl;
  49. cout << "Please enter a Number: ";
  50. cin >> accountnum;
  51. }
  52. //
  53. //
  54. selectionsort(account, SIZE);
  55. //
  56. //Adds Function Results to Variable Status
  57. array = binary(account, SIZE, accountnum);
  58. //
  59. //Determines whether the inputed values are equal to the subscript
  60. if(array == -1)
  61. cout << "\nInvalid." << endl;
  62. else
  63. cout << "\nValid" << endl;
  64. return 0;
  65. }
  66. /*******************************************************************************
  67.  * Adding Definition to the function. This function will search through the list
  68.  * of values in the sorted array by halves and determine whether the value
  69.  * inside matches the inputed value of the user using a binary search.
  70. ******************************************************************************/
  71. int binary(const int a[], int b, int c)
  72. {
  73. int first = 0;
  74. int last = b - 1;
  75. int middle;
  76. int position = -1;
  77. bool found = false;
  78.  
  79. while (!found && first <= last)
  80. {
  81. middle = (first + last) / 2;
  82. if(a[middle] == c)
  83. {
  84. found = true;
  85. position = middle;
  86. }
  87. else if(a[middle] > c)
  88. last = middle - 1;
  89. else
  90. first = middle + 1;
  91. }
  92. return position;
  93. }
  94. /*******************************************************************************
  95.  * Adding definition to Function selectionsort. This function sorts the account
  96.  * amounts from lowest to highest. Uses selection sort to find the lowest
  97.  * value and moves it into the correct position.
  98.  ******************************************************************************/
  99. void selectionsort(int a[], int b)
  100. {
  101. int startscan, minIndex; //Used to scan through the array
  102. int minValue; //Holds lowest value found
  103. //
  104. //Initializing For Loop
  105. for (startscan = 0; startscan < (b - 1); startscan++)
  106. {
  107. minIndex = startscan; //Assigns starting position as lowest
  108. minValue = a[startscan]; //Assigns starting number as lowest
  109. //
  110. //Searches for lowest account number value
  111. for (int index = startscan + 1; index < b; index++)
  112. {
  113. if(a[index] < minValue)
  114. {
  115. minValue = a[index]; //Updates lowest number value
  116. minIndex = index; //stores position of lowest value
  117. }
  118. }
  119. //
  120. //Swaps Values
  121. a[minIndex] = a[startscan];
  122. a[startscan] = minValue;
  123. }
  124. }
  125.  
Success #stdin #stdout 0s 5356KB
stdin
111111
stdout
Please enter a Number: 
Invalid.