fork download
  1. // Kurt Feiereisel CSC5 Chapter 8, p.487, #4
  2. /*******************************************************************************
  3.  *
  4.  * Validate Account Number
  5.  * _____________________________________________________________________________
  6.  * This program allows a user to enter a Charge Account number and the program
  7.  * will determine if the account number entered is valid or invalid.
  8.  * _____________________________________________________________________________
  9.  * INPUT:
  10.  * accNum : Account number of the user
  11.  *
  12.  * OUTPUT:
  13.  * Whether account number inputted is valid or invalid
  14.  *
  15.  *
  16.  * ****************************************************************************/
  17. #include <iostream>
  18. using namespace std;
  19.  
  20. // Function Prototypes
  21. int inputAccNum(int accNum);
  22. void sort(int validAccNums[], int SIZE);
  23. void determine(int validAccNums[], int accNum, int SIZE);
  24.  
  25. int main()
  26. {
  27. // Initalize Variables, Constants, Arrays
  28. int accNum = 0;
  29. const int SIZE = 18;
  30. int validAccNums[SIZE] = {5658845, 4520125, 7895122, 8777541, 8451277,
  31. 1302850, 8080152, 4562555, 5552012, 5050552,
  32. 7825877, 1250255, 1005231, 6545231, 3852085,
  33. 7576651, 7881200, 4581002};
  34. // Function Call
  35. accNum = inputAccNum(accNum);
  36. sort(validAccNums, SIZE);
  37. determine(validAccNums, accNum, SIZE);
  38. return 0;
  39. }
  40. /*
  41.  * Definition of inputAccNum:
  42.  * This function allows a user to enter their charge account number. This
  43.  * function returns an int (num) back to main.
  44.  */
  45. int inputAccNum(int num)
  46. {
  47. // Input num
  48. cout << "Please enter your Charge Account Number: " << endl;
  49. cin >> num;
  50. return num;
  51. }
  52.  
  53. /*
  54.  * Definition of Function: sort
  55.  * This function will sort the array validAccNums into ascending order
  56.  */
  57. void sort(int v[], int size)
  58. {
  59. // Declare Local Variables
  60. int scan;
  61. int minIndex;
  62. int minNum;
  63.  
  64. // For loop to Sort Array using a selection sort
  65. for (scan = 0; scan < (size - 1); scan++)
  66. {
  67. minIndex = scan;
  68. minNum = v[scan];
  69. for(int index = scan + 1; index < size; index++)
  70. {
  71. if(v[index] < minNum)
  72. {
  73. minNum = v[index];
  74. minIndex = index;
  75. }
  76. }
  77. v[minIndex] = v[scan];
  78. v[scan] = minNum;
  79. }
  80. }
  81. /*
  82.  * Definition of Function: determine
  83.  * This function determines whether the account number entered is a valid
  84.  * number. It will then report whether the number is valid or not valid
  85.  */
  86. void determine(int v[], int acc, int size)
  87. {
  88. // Initalize Local Variables
  89. int first = 0; // First array element
  90. int last = size - 1; // Last array element
  91. int middle; // Midpoint of search
  92. int position = -1; // Position of search Value
  93. bool found = false; // Flag
  94.  
  95. while (!found && first <= last)
  96. {
  97. middle = (first + last) / 2;
  98. if(v[middle] == acc)
  99. {
  100. found = true;
  101. position = middle;
  102. }
  103. else if(v[middle] > acc)
  104. last = middle - 1;
  105. else
  106. first = middle + 1;
  107. }
  108. if(position == -1)
  109. cout << "Invalid Account Number." << endl;
  110. else
  111. cout << "Valid Account Number." << endl;
  112. }
  113.  
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
Standard output is empty