fork download
  1. /*******************************************************************************
  2.  * Aiden Stannard // CS1A // Chapter 8 Homework: Challange 1# // 11/7/2026
  3.  *
  4.  * Charge Amount Validation
  5.  *
  6.  * This program validates user entered charge account number by checking it
  7.  * from a list of 18 valid account numbers.
  8.  *
  9.  * Inputs:
  10.  * Enter a charge account number__
  11.  * Output:
  12.  * This number is ___("invalid" or "valid")
  13.  ******************************************************************************/
  14. #include <iostream>
  15. using namespace std;
  16.  
  17. // Function to perform a simple linear search on a single-dimensional array
  18. bool linearSearch(const int [], int, int);
  19.  
  20. int main()
  21. {
  22. // Array size declaration
  23. const int ARRAY_SIZE = 18;
  24. // account numbers
  25. int validAccounts[ARRAY_SIZE] = {
  26. 5658845, 4520125, 7895122, 8777541, 8451277, 1302850,
  27. 8080152, 4562555, 5552012, 5050552, 7825877, 1250255,
  28. 1005231, 6545231, 3852085, 7576651, 7881200, 4581002
  29. };
  30.  
  31. int userNumber;
  32.  
  33. cout << "Enter a charge account number: ";
  34. cin >> userNumber;
  35.  
  36. // Determine if the number is valid using the linear search function
  37. if (linearSearch(validAccounts, ARRAY_SIZE, userNumber))
  38. {
  39. cout << "The number is valid." << endl;
  40. }
  41.  
  42. else
  43. {
  44. cout << "The number is invalid." << endl;
  45. }
  46.  
  47. return 0;
  48. }
  49.  
  50. bool linearSearch(const int accounts[], int size, int target)
  51. {
  52. for (int i = 0; i < size; i++)
  53. {
  54. if (accounts[i] == target)
  55. {
  56. return true; // Match found
  57. }
  58. }
  59. return false; // returns false bool value if if statement does not apply
  60. }
  61.  
Success #stdin #stdout 0s 5344KB
stdin
 5658845
stdout
Enter a charge account number: The number is valid.