fork download
  1. // Wyatt Carey CS1A Chapter 8, P. 487, #1
  2. /*******************************************************************************
  3.  * Charge Account Number
  4.  * _____________________________________________________________________________
  5.  * This program has an single-dimensional array, containing a linear search to
  6.  * find the account number inputted. It will then display the number if its
  7.  * valid or show a message indicating if its not valid.
  8.  * _____________________________________________________________________________
  9.  *
  10.  * INPUTS
  11.  * account number
  12.  *
  13.  * OUTPUTS
  14.  * display message stating it's valid or not valid
  15.  * ****************************************************************************/
  16. #include <iostream>
  17. using namespace std;
  18.  
  19. // Function Prototype
  20. int searchList(const int [], int, int); // Will become function header
  21. const int NUMBER = 126; // Size of the array
  22.  
  23. int main()
  24. {
  25. // Variable Dictionary
  26. int acc[NUMBER] = {5658845, 4520125, 7895122, 8777541, 8451277, 1302850,
  27. 8080152, 4562555, 5552012, 5050552, 7825877, 1250255,
  28. 1005231, 6545231, 3852085, 7576651, 7881200, 4581002};
  29. int acNum; // Cin input
  30. int account_number; // Assigning variable to acNum
  31.  
  32. // Ask For Account Number.
  33. cout << "What is your account number? \n";
  34. cin >> acNum;
  35.  
  36. // Search Array For acNum.
  37. account_number = searchList(acc, NUMBER, acNum);
  38.  
  39. // If searchList Returned -1, Then acNum Wasn't Found.
  40. if (account_number == -1)
  41. cout << "Invalid account number. Type in correct account number" << endl;
  42. else
  43. {
  44. cout << "This is your account number: " << acNum << endl;
  45. }
  46. return 0;
  47. }
  48.  
  49. //******************************************************************************
  50. // The searchList functions will perform a linear search to find the account *
  51. // number inputted. If its not found, the array subscript is returned. *
  52. //******************************************************************************
  53.  
  54. int searchList(const int list[], int numElems, int value)
  55. {
  56. int index = 0;
  57. int position = -1;
  58. bool found = false;
  59.  
  60. // Linear search
  61. while (index < numElems && !found)
  62. {
  63. if (list [index] == value)
  64. {
  65. found = true;
  66. position = index;
  67. }
  68. index++;
  69. }
  70. return position;
  71. }
Success #stdin #stdout 0.01s 5304KB
stdin
8080152
stdout
What is your account number? 
This is your account number: 8080152