fork download
  1. //Sam Trivikraman CS1A Chapter 8, p. 487, #4
  2. //
  3. /*
  4.  ******************************************************************************
  5. Validate Account Numbers (Modification)
  6. _______________________________________________________________________________
  7. This program validates whether the number inputted is a valid account number.
  8. _______________________________________________________________________________
  9. INPUT
  10. the user's account number : The account number that the program is seraching for (user input)
  11.  
  12. OUTPUT
  13. valid/invalid : Whether or not the number that is inputted is in the array
  14. _______________________________________________________________________________
  15. *******************************************************************************
  16. */
  17. //
  18.  
  19.  
  20. #include <iostream>
  21. using namespace std;
  22.  
  23. //a binary search function to help look for the user inputted value
  24. int binary(int arr[], int num, int SIZE)
  25. {
  26. int firstPos = 0; //the first position in the array
  27. int lastPos = SIZE - 1; //the last position of the array
  28. int middlePos; //the middle position of the array
  29. int position = -1; //the position of the inputted value
  30. bool found = false; //a boolean flag
  31.  
  32. //search for the value
  33. while (!found && firstPos <= lastPos)
  34. {
  35. middlePos = (firstPos + lastPos) / 2;
  36. if(arr[middlePos] == num)
  37. {
  38. found = true;
  39. position = middlePos;
  40. }
  41. else if(arr[middlePos] > num)
  42. {
  43. lastPos = middlePos - 1;
  44. }
  45. else
  46. {
  47. firstPos = middlePos + 1;
  48. }
  49. //return the position of the value
  50. return position;
  51. }
  52. return 0;
  53. }
  54.  
  55. //a selection sort which helps to sort the array of account numbers
  56. void selectionSort(int arr[], int SIZE)
  57. {
  58. int start; //the starting position
  59. int minInd; //the index of the minimum value
  60. int minVal; //the value of the smallest number
  61.  
  62. //sort through the array
  63. for(start = 0; start < (SIZE - 1); start++)
  64. {
  65. minInd = start;
  66. minVal = arr[start];
  67.  
  68. for(int index = start + 1; index < SIZE; index++)
  69. {
  70. if(arr[index] < minVal)
  71. {
  72. minVal = arr[index];
  73. minInd = index;
  74. }
  75. }
  76. arr[minInd] = arr[start];
  77. arr[start] = minVal;
  78. }
  79. }
  80.  
  81. int main() {
  82.  
  83. //Data Dictionary
  84. int userAccNum; //INPUT the user's account number
  85. int SIZE = 18; //CONSTANT the size of the array
  86. //INPUT the array that contains all of the account numbers
  87. int accNumArray[SIZE] = {5658845, 4520125 , 7895122 , 8777541 , 8451277, 1302850, 8080152, 4562555, 5552012, 5050552, 7825877, 1250255, 1005231, 6545231, 3852085, 7576651, 7881200, 4581002};
  88.  
  89. //Ask the user for the input (account number)
  90. cout << "Please enter the user number you are looking for." << endl;
  91. cin >> userAccNum;
  92.  
  93. //Sort the list of account numbers in ascending order
  94. selectionSort(accNumArray, SIZE);
  95. //Implement a binary search to look for the number
  96. //Output whether or not the number was found
  97. cout << binary(accNumArray, userAccNum, SIZE) << endl;
  98.  
  99. return 0;
  100. }
Success #stdin #stdout 0.01s 5296KB
stdin
8451277
stdout
Please enter the user number you are looking for.
-1