fork download
  1. //Ava Huntington CS1A Chapter 8 Homework P. 487, #4
  2. //
  3. /*******************************************************************************
  4.  * VALIDATE CHARGE ACCOUNT NUMBERS
  5.  * _____________________________________________________________________________
  6.  * This program will accept a charge account number and determine whether or not
  7.  * the given number matches one in the system using a binary search.
  8.  * _____________________________________________________________________________
  9.  * INPUT
  10.  * number of accounts: given number of accounts in the system
  11.  * account number: specific account number being searched for
  12.  *
  13.  * OUTPUT
  14.  * result: true or false if account is valid or not
  15.  ******************************************************************************/
  16. #include <iostream>
  17. using namespace std;
  18.  
  19. void selectionSort(int list[], int size);
  20. int searchAccountsBinary(const int[], int, int);
  21. const int SIZE = 18;
  22.  
  23. int main()
  24. {
  25. int accounts[SIZE] = {5658845, 8080152, 1005231, 4520125, 4562555, 6545231,
  26. 7895122, 5552012, 3852085, 8777541, 5050552, 7576651, 8451277, 7825877,
  27. 7881200, 1302850, 1250255, 4581002};
  28. int result; //Attempts to match given number with stored one
  29. int accountNumber; // Given charge account number
  30.  
  31. cout << "Enter charge account number:" << endl;
  32. cin >> accountNumber;
  33.  
  34. //Sort account numbers
  35. selectionSort(accounts, SIZE);
  36.  
  37. //Search for account number
  38. result = searchAccountsBinary(accounts, SIZE, accountNumber);
  39.  
  40. if(result == -1)
  41. cout << "Account number " << accountNumber << " is invalid." << endl;
  42. else {
  43. cout << "Account number " << accountNumber <<" is valid." << endl;
  44.  
  45. }
  46. return 0;
  47. }
  48. void selectionSort(int list[], int size)
  49. {
  50. int startScan;
  51. int minIndex;
  52. int minValue;
  53.  
  54. for (startScan = 0; startScan < (size - 1); startScan++)
  55. {
  56. minIndex = startScan;
  57. minValue = list[startScan];
  58. for(int index = startScan + 1; index < size; index++)
  59. {
  60. if(list[index] < minValue)
  61. {
  62. minValue = list[index];
  63. minIndex = index;
  64. }
  65. }
  66. list[minIndex] = list[startScan];
  67. list[startScan] = minValue;
  68. }
  69. }
  70. int searchAccountsBinary(const int list[], int numElms, int value)
  71. {
  72. int first = 0;
  73. int last = numElms -1;
  74. int middle;
  75. int position = -1;
  76. bool found = false;
  77.  
  78. while(!found && first <= last)
  79. {
  80. middle = (first + last) / 2;
  81. if(list[middle] == value)
  82. {
  83. found = true;
  84. position = middle;
  85. }
  86. else if (list[middle] > value)
  87. last = middle - 1;
  88. else
  89. first = middle + 1;
  90. }
  91. return position;
  92. }
  93.  
  94.  
Success #stdin #stdout 0.01s 5284KB
stdin
5658845
stdout
Enter charge account number:
Account number 5658845 is valid.