fork download
  1. //hudson price CSC1A Chapter 8
  2. //
  3. /**************************************************************
  4.  *
  5.  * Charge Account Validation modifacation
  6.  * ____________________________________________________________
  7.  * this program allows the user to put in the charge
  8.  * account number and see if it is valid orn ot
  9.  * ____________________________________________________________
  10.  * INPUT
  11.  *any of the account numbers
  12.  *
  13.  * OUTPUT
  14.  * Valid
  15.  * Invalid
  16.  **************************************************************/
  17. * #include <iostream>
  18. #include <algorithm>
  19.  
  20. using namespace std;
  21.  
  22. // Function to perform selection sort on an array
  23. void selectionSort(int arr[], int size) {
  24. for (int i = 0; i < size - 1; ++i) {
  25. int minIndex = i;
  26. for (int j = i + 1; j < size; ++j) {
  27. if (arr[j] < arr[minIndex]) {
  28. minIndex = j;
  29. }
  30. }
  31. if (minIndex != i) {
  32. swap(arr[i], arr[minIndex]);
  33. }
  34. }
  35. }
  36.  
  37. // fungion to do binary search in array
  38. bool isValidAccount(int accountNumber, int accountArray[], int arraySize) {
  39. int left = 0;
  40. int right = arraySize - 1;
  41.  
  42. while (left <= right) {
  43. int mid = left + (right - left) / 2;
  44.  
  45. if (accountArray[mid] == accountNumber) {
  46. return true; // Account number found
  47. } else if (accountArray[mid] < accountNumber) {
  48. left = mid + 1;
  49. } else {
  50. right = mid - 1;
  51. }
  52. }
  53. return false; // Account number not found
  54. }
  55.  
  56. int main() {
  57. //initialization with lucky numbers
  58. int chargeAccounts[] = {5658845, 4520125, 7895122, 8777541, 8451277, 1302850,
  59. 8080152, 4562555, 5552012, 5050552, 7825877, 1250255,
  60. 1005231, 6545231, 3852085, 7576651, 7881200, 4581002};
  61. // Size of the array
  62. int arraySize = sizeof(chargeAccounts) / sizeof(chargeAccounts[0]);
  63.  
  64. // sorting array with selection sort
  65. selectionSort(chargeAccounts, arraySize);
  66.  
  67. // account numbers imput
  68. cout << "Enter the charge account number: ";
  69. int inputAccountNumber;
  70. cin >> inputAccountNumber;
  71.  
  72. // account number validation
  73. if (isValidAccount(inputAccountNumber, chargeAccounts, arraySize)) {
  74. cout << "Valid account number.\n";
  75. } else {
  76. cout << "Invalid account number.\n";
  77. }
  78.  
  79. return 0;
  80. }
  81.  
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
Enter the charge account number: Invalid account number.