fork download
  1. // Andres Guzman CSC5 Chapter 8, P. 487, #1
  2. /******************************************************************************
  3.  *
  4.  * DETERMINE NUMBER VALIDATION
  5.  * ____________________________________________________________________________
  6.  * This program will determine whether the inputed numbers in the account match
  7.  * those of the valid numbers.
  8.  *
  9.  * Computation:
  10.  *
  11.  * Uses a linear search in the linear function to compare the values in the
  12.  * subscripts to the inputed values of the user. If equal, returns a valid
  13.  * status, and a negative status otherwise.
  14.  * ____________________________________________________________________________
  15.  *
  16.  * INPUT
  17.  * account = The valid numbers
  18.  * accountnum = Holds the inputed value for account numbers
  19.  *
  20.  * OUTPUT
  21.  * array = Displays the status of the inputed numbers
  22.  *
  23.  ******************************************************************************/
  24. #include <iostream>
  25. using namespace std;
  26.  
  27. int linear(const int a[], int b, int c); //Function Prototype
  28. const int SIZE = 18;
  29. int main()
  30. {
  31. //
  32. //Initialization List
  33. int account[SIZE] = {5658845, 4520125, 7895122, 8777541, 8451277, 1302850,
  34. 8080152, 4562555, 5552012, 5050552, 7825877, 1250255,
  35. 1005231, 6545231, 3852085, 7576651, 7881200, 4581002};
  36. int accountnum; //Holds Input Value for users account numbers
  37. int array; //Holds the results of the numbers
  38. //
  39. //Prompt Input Display
  40. cout << "Please enter a Number: ";
  41. cin >> accountnum;
  42. //
  43. //Input Validation
  44. while(accountnum < 0)
  45. {
  46. cout << "Cannot Be Negatives. Please Try Again." << endl;
  47. cout << "Please enter a Number: ";
  48. cin >> accountnum;
  49. }
  50. //
  51. //Adds Function Results to Variable Status
  52. array = linear(account, SIZE, accountnum);
  53. //
  54. //Determines whether the inputed values are equal to the subscript
  55. if(array == -1)
  56. cout << "\nInvalid." << endl;
  57. else
  58. cout << "\nValid" << endl;
  59. return 0;
  60. }
  61. /*******************************************************************************
  62.  * Adding Definition to the function. This function will search through the list
  63.  * of values in the array subscript and determine whether the value inside
  64.  * matches the inputed value of the user, in which case will then determine the
  65.  * status of the input numbers by using a linear search.
  66. ******************************************************************************/
  67. int linear(const int a[], int b, int c)
  68. {
  69. int index = 0;
  70. int position = -1;
  71. bool found = false;
  72.  
  73. while (index < b && !found)
  74. {
  75. if(a[index] == c)
  76. {
  77. found = true;
  78. position = index;
  79. }
  80. index++;
  81. }
  82. return position;
  83. }
  84.  
Success #stdin #stdout 0s 5344KB
stdin
5658845
stdout
Please enter a Number: 
Valid