fork download
  1. // Natalie Zarate CSC5 Chapter 8, P. 487, #4
  2. /*******************************************************************************
  3.  *
  4.  * DETERMINE NUMBER VALIDITY
  5.  *
  6.  * This program will determine whether or not the user's input is valid using an
  7.  * existing database. It will accept as input a positive integer then confirm
  8.  * whether or not the entered value is vaid or invalid.
  9.  *______________________________________________________________________________
  10.  * INPUT
  11.  *
  12.  * accountNums[elements] - reference account numbers
  13.  * validNum - determining value for validity
  14.  * userAccNum - inputted account number
  15.  * AccountNumSort(accounts[], size) - function, sorts variables
  16.  * sort - value that determines when values
  17.  * are moved to be sorted in an array
  18.  * keepVal - refernce value for comarison
  19.  * first - first array element
  20.  * last - last array element
  21.  * middle - midpoint array element; reference
  22.  * point
  23.  * AccountValid(accounts[], size, number) - function, determines if inputted
  24.  * account number is valid.
  25.  * found - indicates if inputted account number
  26.  * is valid
  27.  *
  28.  * OUTPUT
  29.  *
  30.  * ValidNum - determines if inputted account value
  31.  * is valid
  32.  ******************************************************************************/
  33. #include <iostream>
  34. using namespace std;
  35.  
  36. // Prototype for AccountValid
  37. int AccountValid(int accounts[], int size, int number);
  38.  
  39. // Prototype for AccountNumSort
  40. void AccountNumSort(int accounts[], int size);
  41.  
  42. int main()
  43. {
  44.  
  45. int elements = 16;
  46.  
  47. // INPUT - reference account numbers
  48. int accountNums[elements] = {5658845, 4520125, 7895122, 8777541, 8451277,
  49. 1302850, 8089152, 4562555, 5552012, 5050552,
  50. 7825877, 1250255, 1005231, 3852085, 7881200,
  51. 4581002};
  52.  
  53. int validNum; // INPUT - OUTPUT - determines if inputted account value
  54. // is valid
  55. int userAccNum; // INPUT - user's account number
  56.  
  57. // Prompt user for account number
  58. cout << "Enter Charge Account Number: " << endl;
  59. cin >> userAccNum;
  60.  
  61. // Rearrange array
  62. AccountNumSort(accountNums, elements);
  63.  
  64. // Check if valid account number; Call AccountValid
  65. validNum = AccountValid (accountNums,elements, userAccNum);
  66.  
  67. if (validNum == -1)
  68. cout << "Invalid Number" << endl;
  69.  
  70. else
  71. cout << "Valid Number" << endl;
  72.  
  73. return 0;
  74. }
  75.  
  76. /*******************************************************************************
  77.  * AccountNumSort(accounts, size)
  78.  *
  79.  * This function will take the database and sort it from least to greatest.
  80.  * _____________________________________________________________________________
  81.  * INPUT
  82.  *
  83.  * sort - value that flags when values are moved for sorting in database
  84.  * keepVal - refernce value for comarison/sorting
  85.  *
  86.  * OUTPUT
  87.  *
  88.  * account[size] - sorted database, database of account number
  89.  ******************************************************************************/
  90. void AccountNumSort(int accounts[], int size)
  91. {
  92. bool sort; // INPUT - value that flags when values are moved for sorting
  93. // in database
  94. int keepVal; // INPUT - refernce value for comarison/sorting
  95.  
  96. do
  97. {
  98. // initialize sort as a boolean expression
  99. sort = false;
  100. for (int i = 0; i < (size - 1); i++)
  101. {
  102. if (accounts[i] > accounts[i + 1])
  103. {
  104. keepVal = accounts[i];
  105. accounts[i] = accounts[i+1];
  106. accounts [i+1] = keepVal;
  107. sort = true;
  108. }
  109. }
  110. }
  111. while (sort);
  112. }
  113. /*******************************************************************************
  114.  * AccountValid(accounts[], size, number)
  115.  *
  116.  * This function will determine whether or not the user inputted value is valid
  117.  * or not.
  118.  * _____________________________________________________________________________
  119.  * INPUT
  120.  *
  121.  * first - first account number in database
  122.  * last - last account number in database
  123.  * middle - midpoint reference account number
  124.  * found - determinator for number validity
  125.  *
  126.  * OUTPUT
  127.  *
  128.  * position - reference variable, dictates input validity
  129.  ******************************************************************************/
  130. int AccountValid(int accounts[], int size, int number)
  131. {
  132. int first = 0; // INPUT - first account number in database
  133. int last = size - 1; // INPUT - last account number in database
  134. int middle; // INPUT - midpoint reference account number
  135. bool found; // INPUT - determinate for number validity
  136. int position = -1; // OUTPUT - reference variable, dictates input validity
  137.  
  138. // Initialize found
  139. found = false;
  140.  
  141. while (!found && first <= last)
  142. {
  143. // find midpoint value
  144. middle = (first + last) / 2;
  145.  
  146. // if account number is midpoint
  147. if (accounts[middle] == number)
  148. {
  149. found = true;
  150. position = middle;
  151. }
  152.  
  153. // if account number if found in lower half of array
  154. else if (accounts[middle] > number)
  155. // Reinitate value
  156. last = middle - 1;
  157.  
  158. // if account number is in upper half of array
  159. else
  160. first = middle + 1;
  161. }
  162. return position;
  163. }
Success #stdin #stdout 0.01s 5252KB
stdin
5658845
stdout
Enter Charge Account Number: 
Valid Number