fork download
  1. //Jonathan Estrada CSC5 Chapter 8, P.487, #4
  2. /*******************************************************************************
  3.  * VALIDATE ACCOUNT NUMBERR
  4.  * _____________________________________________________________________________
  5.  * This program will determine if the users account number is in the system or
  6.  * not. It will notify the user whether the number went through
  7.  * _____________________________________________________________________________
  8.  * INPUTS
  9.  * accountNumber : Account Numbers
  10.  * SIZE : array size
  11.  * userAccount : Users account number
  12.  *
  13.  * OUTPUT
  14.  *
  15.  *
  16.  *******************************************************************************/
  17. #include <iostream>
  18. using namespace std;
  19.  
  20. int accountSearch( const int[], int, int);
  21. void selectonSort(int[], int);
  22. const int SIZE = 18;
  23.  
  24. int main() {
  25.  
  26. int accountNumber[SIZE] = {5658845, 4530125, 7895122, 8777541, 8451277,
  27. 1302850, 8080152, 4562555, 5552012, 5050552, 7825877, 1250255, 1005231,
  28. 6545231, 3852085, 7576651, 7881200, 4581002};
  29.  
  30. int userAccount;
  31. int result;
  32.  
  33. selectonSort(accountNumber, SIZE);
  34.  
  35. cout << "Please enter number associated with the account: ";
  36. cin >> userAccount;
  37. cout << userAccount << endl;
  38.  
  39. result = accountSearch(accountNumber, SIZE, userAccount);
  40.  
  41. if(result == -1)
  42. cout << "Account Number is not valid." << endl;
  43. else
  44. cout << "Account Number was found and valid." << endl;
  45.  
  46. return 0;
  47. }
  48. /*******************************************************************************
  49.  * Definition of function selectionSort.
  50.  * This function will sort all the account numbers for use of the binary search
  51.  * later on.
  52.  * *****************************************************************************/
  53. void selectonSort(int accountNumber[], int SIZE)
  54. {
  55. int startScan, minIndex, minValue;
  56.  
  57. for(startScan = 0; startScan < (SIZE + 1); startScan++)
  58. {
  59. minIndex = startScan;
  60. minValue = accountNumber[startScan];
  61. for(int index = startScan +1; index < SIZE; index++)
  62. {
  63. if(accountNumber[index] < minValue)
  64. {
  65. minValue = accountNumber[index];
  66. minIndex = index;
  67. }
  68. }
  69. accountNumber[minIndex] = accountNumber[startScan];
  70. accountNumber[startScan] = minValue;
  71. }
  72. }
  73. /*******************************************************************************
  74.  * Definiton of function accountSearch
  75.  * This funcion will conduct a binary search to determine if the user inputted
  76.  * a valid account number or not.
  77.  * *****************************************************************************/
  78. int accountSearch(const int accountSearch[], int SIZE, int userAccount)
  79. {
  80. int first = 0;
  81. int last = SIZE - 1;
  82. int middle;
  83. int positon = - 1;
  84. bool found = false;
  85.  
  86. while(!found && first <= last)
  87. {
  88. middle = (first + last)/2;
  89. if(accountSearch[middle] == userAccount)
  90. {
  91. found = true;
  92. positon = middle;
  93. }
  94. else if (accountSearch[middle]>userAccount)
  95. last = middle - 1;
  96. else
  97. first = middle + 1;
  98. }
  99. return positon;
  100. }
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
Please enter number associated with the account: 22070
Account Number is not valid.