-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3d6471d
commit 99c9b07
Showing
21 changed files
with
1,696 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
91 changes: 91 additions & 0 deletions
91
...nd-Sorting-Arrays/8.5 Sorting and Searching vectors (Continued from Section 7.11)/8-7.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
#include <iostream> | ||
#include <string> | ||
#include <vector> | ||
using namespace std; | ||
|
||
void selectionSort(vector<string> &); | ||
void swap(string &, string &); | ||
int binarySearch(const vector<string> &, string); | ||
|
||
int main() | ||
{ | ||
string search_value; | ||
int position; | ||
|
||
vector<string> names{"Lopez", "Smith", "Pike", "Jones", | ||
"Abernathy", "Hall", "Wilson", "Kimura", | ||
"Alvarado", "Harrison", "Geddes", "Irvine" }; | ||
|
||
selectionSort(names); | ||
|
||
cout << "Here are the sorted names: " << endl; | ||
for (auto element : names) | ||
cout << element << endl; | ||
cout << endl; | ||
|
||
cout << "Enter a name to search for: "; | ||
getline(cin, search_value); | ||
position = binarySearch(names, search_value); | ||
|
||
if (position != -1) | ||
cout << "That name is found at position " << position << endl; | ||
else | ||
cout << "That name is not found.\n"; | ||
|
||
return 0; | ||
} | ||
|
||
void selectionSort(vector<string> &vector_array) | ||
{ | ||
int min_index; | ||
string min_value; | ||
|
||
for (int start_index = 0; start_index < (vector_array.size() - 1); start_index++) | ||
{ | ||
min_index = start_index; | ||
min_value = vector_array[start_index]; | ||
|
||
for (int index = start_index + 1; index < vector_array.size(); index++) | ||
{ | ||
if (vector_array[index] < min_value) | ||
{ | ||
min_value = vector_array[index]; | ||
min_index = index; | ||
} | ||
|
||
} | ||
swap(vector_array[min_index], vector_array[start_index]); | ||
} | ||
} | ||
|
||
void swap(string &a, string &b) | ||
{ | ||
string temp = a; | ||
a = b; | ||
b = temp; | ||
} | ||
|
||
int binarySearch(const vector<string> &vector_array, string user_string) | ||
{ | ||
int first = 0, | ||
last = vector_array.size() - 1, | ||
middle, | ||
position = -1; | ||
bool found = false; | ||
|
||
while (!found && first <= last) | ||
{ | ||
middle = (first + last) / 2; | ||
|
||
if (vector_array[middle] == user_string) | ||
{ | ||
found = true; | ||
position = middle; | ||
} | ||
else if (vector_array[middle] > user_string) | ||
last = middle - 1; | ||
else | ||
first = middle + 1; | ||
} | ||
return position; | ||
} |
17 changes: 17 additions & 0 deletions
17
Chapter-8-Searching-and-Sorting-Arrays/Review Questions and Exercises/Fill-in-the-Blank.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
8. The __linear__ search algorithm steps sequentially through | ||
an array, comparing each item with the search value. | ||
|
||
9. The __binary__ search algorithm repeatedly divides the | ||
portion of an array being searched in half. | ||
|
||
10. The __linear__ search algorithm is adequate for small | ||
arrays but not large arrays. | ||
|
||
11. The __binary__ search algorithm requires that the | ||
array's contents be sorted. | ||
|
||
12. If an array is sorted in __ascending__ order, the | ||
values are stored from lowest to highest. | ||
|
||
13. If an array is sorted in __descending__ order, the | ||
values are stored from highest to lowest. |
12 changes: 12 additions & 0 deletions
12
...Searching-and-Sorting-Arrays/Review Questions and Exercises/Programming Challenges/00.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/** | ||
* 00. | ||
* | ||
**/ | ||
#include <iostream> | ||
using namespace std; | ||
|
||
int main() | ||
{ | ||
|
||
return 0; | ||
} |
70 changes: 70 additions & 0 deletions
70
...Searching-and-Sorting-Arrays/Review Questions and Exercises/Programming Challenges/01.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/** | ||
* 1. Charge Account Validation | ||
* | ||
* Write a program that lets the user enter a charge account number. | ||
* The program should determine if the number is valid by checking for | ||
* it in the following list: | ||
* | ||
* 5658845 4520125 7895122 8777541 8451277 1302850 | ||
* 8080152 4562555 5552012 5050552 7825877 1250255 | ||
* 1005231 6545231 3852085 7576651 7881200 4581002 | ||
* | ||
* The list of numbers above should be initialized in a single-dimensional | ||
* array. A simple linear search should be used to locate the number | ||
* entered by the user. If the user enters a number that is in the array, | ||
* the program should display a message saying that the number is valid. | ||
* If the user enters a number that is not in the array, the program should | ||
* display a message indicating that the number is invalid. | ||
* | ||
**/ | ||
#include <iostream> | ||
using namespace std; | ||
|
||
int inputValidation(int); | ||
bool linearSearch(const int[], const int, const int); | ||
|
||
int main() | ||
{ | ||
const int NUMBER_OF_ACCOUNTS = 18; | ||
|
||
int account_numbers[NUMBER_OF_ACCOUNTS] = { 5658845, 4520125, 7895122, 8777541, 8451277, 1302850, | ||
8080152, 4562555, 5552012, 5050552, 7825877, 1250255, | ||
1005231, 6545231, 3852085, 7576651, 7881200, 4581002 }; | ||
|
||
cout << "Enter an account #: " << endl; | ||
int user_account_number = inputValidation(user_account_number); | ||
|
||
bool user_account_number_is_valid = linearSearch(account_numbers, NUMBER_OF_ACCOUNTS, user_account_number); | ||
|
||
if (user_account_number_is_valid) | ||
cout << "Your account number is valid." << endl; | ||
else | ||
cout << "Your account number is invalid." << endl; | ||
|
||
return 0; | ||
} | ||
|
||
int inputValidation(int user_number) | ||
{ | ||
while (!(cin >> user_number)) | ||
{ | ||
cout << "Error. Enter a valid number: "; | ||
cin.clear(); | ||
cin.ignore(numeric_limits<streamsize>::max(), '\n'); | ||
} | ||
return user_number; | ||
} | ||
|
||
bool linearSearch(const int array[], const int ARRAY_SIZE, const int user_number) | ||
{ | ||
int index = 0; | ||
bool found = false; | ||
|
||
while (index < ARRAY_SIZE && !found) | ||
{ | ||
if (array[index] == user_number) | ||
found = true; | ||
index++; | ||
} | ||
return found; | ||
} |
68 changes: 68 additions & 0 deletions
68
...Searching-and-Sorting-Arrays/Review Questions and Exercises/Programming Challenges/02.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* 2. Lottery Winners | ||
* | ||
* A lottery ticket buyer purchases 10 tickets a week, always | ||
* playing the same 10 5-digit “lucky” combinations. Write a | ||
* program that initializes an array or a vector with these | ||
* numbers and then lets the player enter this week’s winning | ||
* 5-digit number. The program should perform a linear search | ||
* through the list of the player’s numbers and report whether | ||
* or not one of the tickets is a winner this week. Here are | ||
* the numbers: | ||
* | ||
* 13579 26791 26792 33445 55555 | ||
* 62483 77777 79422 85647 93121 | ||
* | ||
**/ | ||
#include <iostream> | ||
#include <vector> | ||
|
||
using namespace std; | ||
|
||
int inputValidate(int, const string); | ||
bool linearSearch(const vector<int>, const int); | ||
|
||
int main() | ||
{ | ||
vector<int> users_lotto_numbers { 13579, 26791, 26792, 33445, 55555, | ||
62483, 77777, 79422, 85647, 93121 }; | ||
|
||
int winning_numbers = inputValidate(winning_numbers, "Enter 5-digit lotto numbers:"); | ||
|
||
bool number_found = linearSearch(users_lotto_numbers, winning_numbers); | ||
|
||
if (number_found) | ||
cout << "Winner!!" << endl; | ||
else | ||
cout << "None of you lotto numbers match the winning numbers." << endl; | ||
|
||
|
||
return 0; | ||
} | ||
|
||
int inputValidate(int user_number, const string prompt) | ||
{ | ||
cout << prompt << endl; | ||
|
||
while (!(cin >> user_number)) | ||
{ | ||
cout << "Error. A number must be entered." << endl; | ||
cout << prompt << endl; | ||
cin.clear(); | ||
cin.ignore(numeric_limits<streamsize>::max(), '\n'); | ||
} | ||
return user_number; | ||
} | ||
|
||
bool linearSearch(const vector<int> vector_array, const int search_term) | ||
{ | ||
bool is_found = false; | ||
for(int i = 0; i < vector_array.size(); i++) | ||
{ | ||
if (search_term == vector_array[i]) | ||
is_found = true; | ||
|
||
} | ||
return is_found; | ||
|
||
} |
71 changes: 71 additions & 0 deletions
71
...Searching-and-Sorting-Arrays/Review Questions and Exercises/Programming Challenges/03.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/** | ||
* 3. Lottery Winners Modification | ||
* | ||
* Modify the program you wrote for Programming Challenge 2 | ||
* (Lottery Winners) so it performs a binary search instead | ||
* of a linear search. | ||
* | ||
**/ | ||
#include <iostream> | ||
#include <vector> | ||
|
||
using namespace std; | ||
|
||
int inputValidate(int, const string); | ||
bool binarySearch(const vector<int>, const int); | ||
|
||
int main() | ||
{ | ||
vector<int> users_lotto_numbers { 13579, 26791, 26792, 33445, 55555, | ||
62483, 77777, 79422, 85647, 93121 }; | ||
|
||
int winning_numbers = inputValidate(winning_numbers, "Enter 5-digit lotto numbers:"); | ||
|
||
bool number_found = binarySearch(users_lotto_numbers, winning_numbers); | ||
|
||
if (number_found) | ||
cout << "Winner!!" << endl; | ||
else | ||
cout << "Not Winner!!" << endl; | ||
|
||
|
||
return 0; | ||
} | ||
|
||
int inputValidate(int user_number, const string prompt) | ||
{ | ||
cout << prompt << endl; | ||
|
||
while (!(cin >> user_number)) | ||
{ | ||
cout << "Error. A number must be entered." << endl; | ||
cout << prompt << endl; | ||
cin.clear(); | ||
cin.ignore(numeric_limits<streamsize>::max(), '\n'); | ||
} | ||
return user_number; | ||
} | ||
|
||
bool binarySearch(const vector<int> vector_array, const int search_term) | ||
{ | ||
int first = 0, | ||
last = vector_array.size() - 1, | ||
middle; | ||
|
||
bool is_found = false; | ||
|
||
while (!is_found && first <= last) | ||
{ | ||
middle = (first + last) / 2; | ||
if(vector_array[middle] == search_term) | ||
is_found = true; | ||
else if (vector_array[middle] > search_term) | ||
last = middle - 1; | ||
else | ||
first = middle + 1; | ||
|
||
} | ||
|
||
return is_found; | ||
|
||
} |
Oops, something went wrong.