fork download
  1. // Nathanael Schwartz CS1A Chapter 8, P. 515, #1
  2. //
  3. /*******************************************************************************
  4.  *
  5.  * VERIFY CHARGE ACCOUNT NUMBER
  6.  * _____________________________________________________________________________
  7.  * This program prompts the user to enter a charge account number and verifies
  8.  * its validity by checking it against a predefined list using a linear search.
  9.  * _____________________________________________________________________________
  10.  * INPUT
  11.  * userAccount : Charge account number input by the user
  12.  *
  13.  * OUTPUT
  14.  * Message indicating if the number is valid or invalid
  15.  *
  16.  ******************************************************************************/
  17. #include <iostream>
  18. using namespace std;
  19.  
  20. // Function prototype
  21. bool linearSearch(const int[], int, int);
  22.  
  23. int main() {
  24. // List of valid charge account numbers
  25. const int SIZE = 18;
  26. int validAccounts[SIZE] = {
  27. 5658845, 4520125, 7895122, 8777541, 8451277, 1302850,
  28. 8080152, 4562555, 5552012, 5050552, 7825877, 1250255,
  29. 1005231, 6545231, 3852085, 7576651, 7881200, 4581002
  30. };
  31.  
  32. int userAccount; // User-entered account number
  33.  
  34. // Prompt the user to enter an account number
  35. cout << "Enter a charge account number: ";
  36. cin >> userAccount;
  37.  
  38. // Search for the account number in the array
  39. if (linearSearch(validAccounts, SIZE, userAccount)) {
  40. cout << "The account number is valid." << endl;
  41. } else {
  42. cout << "The account number is invalid." << endl;
  43. }
  44.  
  45. return 0;
  46. }
  47.  
  48. /**************************************************************
  49.  * linearSearch *
  50.  * This function performs a linear search on an integer array *
  51.  * to find a specified value. Returns true if found; *
  52.  * otherwise, returns false. *
  53.  **************************************************************/
  54. bool linearSearch(const int array[], int size, int value) {
  55. for (int i = 0; i < size; i++) {
  56. if (array[i] == value) {
  57. return true; // Value found, return true
  58. }
  59. }
  60. return false; // Value not found, return false
  61. }
  62.  
Success #stdin #stdout 0.01s 5272KB
stdin
4520125
stdout
Enter a charge account number: The account number is valid.