//Maxwell Brewer CS1A Chapter 8, p. 487, #4
//
/*******************************************************************************
* VALIDATE CHARGE ACCOUNT (MODIFIED)
* _____________________________________________________________________________
* This program checks if a user-entered account number exists in a predefined
* list using sorting and binary search.
* _____________________________________________________________________________
* INPUT
* user : The account number entered by the user
*
* OUTPUT
* Displays whether the entered number is valid or not.
*******************************************************************************/
#include <iostream>
using namespace std;
const unsigned SIZE = 18;
int binarySearch(int, const int*, const unsigned);
void selectionSort(int*, const unsigned);
int main() {
int accounts[SIZE] = {
5658845, 4520125, 7895122, 8777541, 8451277, 1302850,
8080152, 4562555, 5552012, 5050552, 7825877, 1250255,
1005231, 6545231, 3852085, 7576651, 7881200, 4581002
};
int user; // User-entered account number
cout << "Enter your account number: ";
cin >> user;
selectionSort(accounts, SIZE); // Sort the array
if (binarySearch(user, accounts, SIZE) < 0) {
cout << "Sorry, but the account number is invalid.\n";
} else {
cout << "The account number is valid.\n";
}
return 0;
}
void selectionSort(int accounts[], const unsigned SIZE) {
int smallest, temp;
for (unsigned i = 0; i < SIZE - 1; i++) {
smallest = i;
for (unsigned j = i + 1; j < SIZE; j++) {
if (accounts[j] < accounts[smallest]) {
smallest = j;
}
}
// Swap the smallest element with the current element
temp = accounts[smallest];
accounts[smallest] = accounts[i];
accounts[i] = temp;
}
}
int binarySearch(int account, const int accounts[], const unsigned SIZE) {
int first = 0, last = SIZE - 1, mid;
while (first <= last) {
mid = (first + last) / 2;
if (account == accounts[mid]) {
return mid; // Return the index of the found account
} else if (account < accounts[mid]) {
last = mid - 1;
} else {
first = mid + 1;
}
}
return -1; // Return -1 if not found
}
Ly9NYXh3ZWxsIEJyZXdlciAgICAgICAgICAgICAgICAgICBDUzFBICAgICAgICAgICAgICAgICAgICBDaGFwdGVyIDgsIHAuIDQ4NywgIzQKLy8KLyoqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioKICogVkFMSURBVEUgQ0hBUkdFIEFDQ09VTlQgKE1PRElGSUVEKQogKiBfX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fXwogKiBUaGlzIHByb2dyYW0gY2hlY2tzIGlmIGEgdXNlci1lbnRlcmVkIGFjY291bnQgbnVtYmVyIGV4aXN0cyBpbiBhIHByZWRlZmluZWQgCiAqIGxpc3QgdXNpbmcgc29ydGluZyBhbmQgYmluYXJ5IHNlYXJjaC4KICogX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18KICogSU5QVVQKICogICB1c2VyIDogVGhlIGFjY291bnQgbnVtYmVyIGVudGVyZWQgYnkgdGhlIHVzZXIKICogCiAqIE9VVFBVVAogKiAgIERpc3BsYXlzIHdoZXRoZXIgdGhlIGVudGVyZWQgbnVtYmVyIGlzIHZhbGlkIG9yIG5vdC4KICoqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKiovCgojaW5jbHVkZSA8aW9zdHJlYW0+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgpjb25zdCB1bnNpZ25lZCBTSVpFID0gMTg7CgppbnQgYmluYXJ5U2VhcmNoKGludCwgY29uc3QgaW50KiwgY29uc3QgdW5zaWduZWQpOwp2b2lkIHNlbGVjdGlvblNvcnQoaW50KiwgY29uc3QgdW5zaWduZWQpOwoKaW50IG1haW4oKSB7CiAgICBpbnQgYWNjb3VudHNbU0laRV0gPSB7CiAgICAgICAgNTY1ODg0NSwgNDUyMDEyNSwgNzg5NTEyMiwgODc3NzU0MSwgODQ1MTI3NywgMTMwMjg1MCwKICAgICAgICA4MDgwMTUyLCA0NTYyNTU1LCA1NTUyMDEyLCA1MDUwNTUyLCA3ODI1ODc3LCAxMjUwMjU1LAogICAgICAgIDEwMDUyMzEsIDY1NDUyMzEsIDM4NTIwODUsIDc1NzY2NTEsIDc4ODEyMDAsIDQ1ODEwMDIKICAgIH07CiAgICAKICAgIGludCB1c2VyOyAgLy8gVXNlci1lbnRlcmVkIGFjY291bnQgbnVtYmVyCiAgICBjb3V0IDw8ICJFbnRlciB5b3VyIGFjY291bnQgbnVtYmVyOiAiOwogICAgY2luID4+IHVzZXI7CiAgICAKICAgIHNlbGVjdGlvblNvcnQoYWNjb3VudHMsIFNJWkUpOyAgLy8gU29ydCB0aGUgYXJyYXkKICAgIAogICAgaWYgKGJpbmFyeVNlYXJjaCh1c2VyLCBhY2NvdW50cywgU0laRSkgPCAwKSB7CiAgICAgICAgY291dCA8PCAiU29ycnksIGJ1dCB0aGUgYWNjb3VudCBudW1iZXIgaXMgaW52YWxpZC5cbiI7CiAgICB9IGVsc2UgewogICAgICAgIGNvdXQgPDwgIlRoZSBhY2NvdW50IG51bWJlciBpcyB2YWxpZC5cbiI7CiAgICB9CiAgICAKICAgIHJldHVybiAwOwp9Cgp2b2lkIHNlbGVjdGlvblNvcnQoaW50IGFjY291bnRzW10sIGNvbnN0IHVuc2lnbmVkIFNJWkUpIHsKICAgIGludCBzbWFsbGVzdCwgdGVtcDsKICAgIAogICAgZm9yICh1bnNpZ25lZCBpID0gMDsgaSA8IFNJWkUgLSAxOyBpKyspIHsKICAgICAgICBzbWFsbGVzdCA9IGk7CiAgICAgICAgCiAgICAgICAgZm9yICh1bnNpZ25lZCBqID0gaSArIDE7IGogPCBTSVpFOyBqKyspIHsKICAgICAgICAgICAgaWYgKGFjY291bnRzW2pdIDwgYWNjb3VudHNbc21hbGxlc3RdKSB7CiAgICAgICAgICAgICAgICBzbWFsbGVzdCA9IGo7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgCiAgICAgICAgLy8gU3dhcCB0aGUgc21hbGxlc3QgZWxlbWVudCB3aXRoIHRoZSBjdXJyZW50IGVsZW1lbnQKICAgICAgICB0ZW1wID0gYWNjb3VudHNbc21hbGxlc3RdOwogICAgICAgIGFjY291bnRzW3NtYWxsZXN0XSA9IGFjY291bnRzW2ldOwogICAgICAgIGFjY291bnRzW2ldID0gdGVtcDsKICAgIH0KfQoKaW50IGJpbmFyeVNlYXJjaChpbnQgYWNjb3VudCwgY29uc3QgaW50IGFjY291bnRzW10sIGNvbnN0IHVuc2lnbmVkIFNJWkUpIHsKICAgIGludCBmaXJzdCA9IDAsIGxhc3QgPSBTSVpFIC0gMSwgbWlkOwogICAgCiAgICB3aGlsZSAoZmlyc3QgPD0gbGFzdCkgewogICAgICAgIG1pZCA9IChmaXJzdCArIGxhc3QpIC8gMjsKICAgICAgICAKICAgICAgICBpZiAoYWNjb3VudCA9PSBhY2NvdW50c1ttaWRdKSB7CiAgICAgICAgICAgIHJldHVybiBtaWQ7ICAvLyBSZXR1cm4gdGhlIGluZGV4IG9mIHRoZSBmb3VuZCBhY2NvdW50CiAgICAgICAgfSBlbHNlIGlmIChhY2NvdW50IDwgYWNjb3VudHNbbWlkXSkgewogICAgICAgICAgICBsYXN0ID0gbWlkIC0gMTsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBmaXJzdCA9IG1pZCArIDE7CiAgICAgICAgfQogICAgfQogICAgCiAgICByZXR1cm4gLTE7ICAvLyBSZXR1cm4gLTEgaWYgbm90IGZvdW5kCn0=