fork download
  1. //Andrew Alspaugh CS1A Chapter 8. P. 487
  2. /****************************************************************************
  3. Validate Charge Accout Input
  4. _____________________________________________________________________________
  5. The purpose of this program is to validate an inputted account number
  6.  
  7. This program has an initiallized array with 18 values, the program uses a bubble
  8. sort and then a binary search to check if the inputted charge number is valid
  9. ____________________________________________________________________________
  10. //INPUTS
  11. const int SIZE = 18;
  12. int chargeNumber[SIZE] = {5658845, 4520125, 7895122, 8777541, 8451277, 1302850, 8080152, 456255, 5552012, 505552, 7825877, 1250255, 1005231, 6545231, 3852085, 7576651, 7881200, 4581002};
  13.  
  14. //Charge Account
  15. int userInput;
  16.  
  17. //Bubble Sort
  18. bool swap;
  19. int temp;
  20.  
  21. //Binary Search
  22. int first = 0;
  23. int last = SIZE - 1;
  24. int middle;
  25. int position = -1;
  26. bool found = false;
  27.  
  28. //OUTPUTS
  29. bool valid = false;
  30. *******************************************************************************/
  31. #include <iostream>
  32. using namespace std;
  33.  
  34. int main()
  35. {
  36. //DATA DICTIONARY
  37. //INPUTS
  38. const int SIZE = 18;
  39. int chargeNumber[SIZE] = {5658845, 4520125, 7895122, 8777541, 8451277, 1302850, 8080152, 456255, 5552012, 505552, 7825877, 1250255, 1005231, 6545231, 3852085, 7576651, 7881200, 4581002};
  40.  
  41. //Charge Account
  42. int userInput;
  43.  
  44. //Bubble Sort
  45. bool swap;
  46. int temp;
  47.  
  48. //Binary Search
  49. int first = 0;
  50. int last = SIZE - 1;
  51. int middle;
  52. int position = -1;
  53. bool found = false;
  54.  
  55. //OUTPUTS
  56. bool valid = false;
  57.  
  58. //INPUT
  59. cout << "Enter Charge Number" << endl;
  60. cin >> userInput;
  61.  
  62. //PROCESS
  63. //SORT ARRAY
  64. do
  65. {
  66. swap = false;
  67. for (int count = 0; count < (SIZE -1) ; count ++)
  68. {
  69. if (chargeNumber[count] > chargeNumber[count + 1])
  70. {
  71. temp = chargeNumber[count];
  72. chargeNumber[count] = chargeNumber[count + 1];
  73. chargeNumber[count + 1] = temp;
  74. swap = true;
  75. }
  76. }
  77. } while (swap);
  78. //BINARY SEARCH
  79.  
  80. while(!found && first <= last)
  81. {
  82. middle = (first + last) / 2;
  83. if (chargeNumber[middle] == userInput)
  84. {
  85. found = true;
  86. position = middle;
  87. }
  88. else if (chargeNumber[middle] > userInput)
  89. last = middle - 1;
  90. else
  91. first = middle + 1;
  92. }
  93.  
  94. //OUTPUT
  95. if (found == true)
  96. {
  97. cout << "Charge Number Was Found In Position //AFTER SORT//: " << position << endl;
  98. cout << "Number is Valid" << endl;
  99. cout << chargeNumber[position];
  100. }
  101.  
  102. if (found == false)
  103. {
  104. cout << "Charge Number Not Found" << endl;
  105. cout << "Number is Invalid" << endl;
  106. }
  107. return 0;
  108. }
Success #stdin #stdout 0.01s 5284KB
stdin
5658845
stdout
Enter Charge Number
Charge Number Was Found In Position //AFTER SORT//:  9
Number is Valid
5658845