fork download
  1. //Marvyn Jesse De La Torre CS1A Chapter 8 #1 pg. 487
  2. //
  3. /*************************************************************
  4. *
  5. * Determine Account Validation
  6. *
  7. * This program checks whether an account number is valid
  8. * by searching through a list of accepted account numbers.
  9. *
  10. * Input:
  11. * num: The account number entered by the user
  12. *
  13. * Output:
  14. * Displays whether the account number is valid.
  15. *************************************************************/
  16.  
  17. #include <iostream>
  18. using namespace std;
  19.  
  20. int check(int arr[], int size, int num);
  21.  
  22. int main()
  23. {
  24. int arr[] = {
  25. 5658845, 4520125, 7895122, 8777541, 8451277,
  26. 1302850, 8080152, 4562555, 5552012, 5050552,
  27. 7825877, 1250255, 1005231, 6545231, 3852085,
  28. 7576651, 7881200, 4581002
  29. };
  30.  
  31. int size = sizeof(arr) / sizeof(arr[0]);
  32. int num;
  33.  
  34. cout << "Enter an account number: ";
  35. cin >> num;
  36.  
  37. int position = check(arr, size, num);
  38.  
  39. if (position == -1)
  40. {
  41. cout << "Invalid account number." << endl;
  42. }
  43. else
  44. {
  45. cout << "Valid account number." << endl;
  46. }
  47.  
  48. return 0;
  49. }
  50.  
  51. int check(int arr[], int size, int num)
  52. {
  53. int position = -1;
  54. int index = 0;
  55.  
  56. while (position == -1 && index < size)
  57. {
  58. if (arr[index] == num)
  59. {
  60. position = index;
  61. }
  62.  
  63. index++;
  64. }
  65.  
  66. return position;
  67. }
Success #stdin #stdout 0.01s 5336KB
stdin
5658845
stdout
Enter an account number: Valid account number.