fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. const int SIZE = 18;
  5. int accountNums[SIZE] = {5658845, 4520125, 7895122, 8777541, 8451277, 1302850,
  6. 8080152, 4562555, 5552012, 5050552, 7825877, 1250255,
  7. 1005231, 6545231, 3852085, 7576651, 7881200, 4581002};
  8.  
  9. void selectionSort(int arr[], int size) {
  10. for (int i = 0; i < size - 1; ++i) {
  11. int min = i;
  12. for (int j = i + 1; j < size; ++j) {
  13. if (arr[j] < arr[min]) {
  14. min = j;
  15. }
  16. }
  17. if (min != i) {
  18. swap(arr[i], arr[min]);
  19. }
  20. }
  21. }
  22.  
  23. bool binarySearch(int arr[], int size, int target) {
  24. int left = 0;
  25. int right = size - 1;
  26. while (left <= right) {
  27. int mid = left + (right - left) / 2;
  28. if (arr[mid] == target) {
  29. return true;
  30. } else if (arr[mid] < target) {
  31. left = mid + 1;
  32. } else {
  33. right = mid - 1;
  34. }
  35. }
  36. return false;
  37. }
  38.  
  39. int main() {
  40. int inputNumber;
  41. cout << "Enter a charge account number: ";
  42. cin >> inputNumber;
  43.  
  44. selectionSort(accountNums, SIZE);
  45.  
  46. if (binarySearch(accountNums, SIZE, inputNumber)) {
  47. cout << "The account number is valid." << endl;
  48. } else {
  49. cout << "The account number is invalid." << endl;
  50. }
  51.  
  52. return 0;
  53. }
  54.  
Success #stdin #stdout 0s 5288KB
stdin
5552012
stdout
Enter a charge account number: The account number is valid.