1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
#include <iostream>
using namespace std;
const int SIZE = 18;
class AccountNumber
{
private:
int valueKey;
int accNums[18];
public:
AccountNumber();
void setValueKey(int k)
{
valueKey = k;
}
int linearSearch();
};
AccountNumber::AccountNumber()
{
accNums[18] = { 5658845, 4520125, 7895122, 8777541, 8451277, 1302850,
8080152, 4562555, 5552012, 5050552, 7825877, 1250255,
1005231, 654231, 3852085, 7576651, 7881299, 4581002 };
}
int AccountNumber::linearSearch()
{
int index = 0;
int position = -1;
bool found = false;
while (index < SIZE && !found)
{
if (accNums[index] == valueKey)
{
found = true;
position = index;
}
index++;
}
return position;
}
void displayResults(const int, const int);
int main()
{
AccountNumber obj;
int userChoice;
int results;
cout << "Enter a charge account number: ";
cin >> userChoice;
obj.setValueKey(userChoice);
results = obj.linearSearch();
displayResults(userChoice, results);
return 0;
}
void displayResults(const int userChoice, const int results)
{
if (results == -1)
cout << "\nThe charge account number is invalid.\n";
else
{
cout << "\nThe charge account number " << userChoice << " was found\n"
<< "in subscript " << results << " of the array.\n";
}
}
| |