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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
#include <iostream>
#include <string>
using namespace std;
// functions
void stringSort(string[], int);
bool binarySearch(const string[], string, int);
int main()
{
const int NUM_NAMES = 20; // number of elements in the array
string names [NUM_NAMES] = {"Smith, John", "Newsad, Kaitlyn", "Doe, John", "Doe, Jane"}; // the string of names
string searchName; // the name the user wants to search for
//call sorting function on array names
stringSort(names, NUM_NAMES);
// Ask user what name they want to serahc
cout << "This program has you enter a person's last and first name. Then see's if name entered is in the array.\n";
cout << "Enter the name to search (ex: Smith, John):\n";
// get the input name using getline function
getline(cin, searchName);
// call function to binary search input name
// and display message according to function output
if (binarySearch(names, searchName, NUM_NAMES))
cout << searchName << " was found in list!\n"; // if the searchName was found in the array
else
cout << searchName << " was not found!\n"; // if the searchName was not found in the array
return 0; // return zero;
}
// ---------------------------------------------------------------
// stringSort: Sort the names in ascending order.
// string names []: array of names
// arraySize: the size of the array
// ---------------------------------------------------------------
void stringSort(string names[], int arraySize) {
// Declare variables
int startArray; // start of the array
// Store varibales temporary in minIdex and minName
int minIndex; // min number of the index
string minName; // min name of the index
// for loop for all elements until second to last
for (startArray = 0; startArray < arraySize - 1; startArray++) {
// initialize minValue and minIndex to first element
minName = names[startArray];
minIndex = startArray;
// for loop on all remaining elements of array
for (int index = startArray + 1; index < arraySize; index++) {
// see if current string element is less than current minValue
if (names[index] < minName) {
//update minValue and minIndex to values of current element
minName = names[index];
minIndex = index;
}
} //inner for loop ends here
// the current [startScan] element to its final position
names[minIndex] = names[startArray];
// lowest value first
names[startArray] = minName;
}
}
//---------------------------------------------------------------------------
// binarySearch: searches the array names[first] throught names[last].
// const string names[]: the array to search throught
// searchName: the name that was search
// arraySize: the size of the array
// returns false if user name/element number was not found
// --------------------------------------------------------------------------
bool binarySearch(const string names[], string searchName, int arraySize) {
// Declare variables
int first = 0; // first index of the element
int last = arraySize - 1; // last index of the element
int middle; // middle index of the element
// While loop until first and last indexes have not overlapped
while (first <= last) {
middle = (first + last) / 2; // get the middle index
// if middle elemnet equals the searchName then return true
if (names[middle] == searchName) {
return true;
}
// if middle element is greater than searchName
// then update the 1st part of the array search
else if (names[middle] > searchName) {
last = middle - 1;
}
// if middle element is less than searchName
// update search space to 2nd half of the array
else if (names[middle] < searchName) {
first = middle + 1;
}
}
// if loop has finished and the number has not been found, return false
return false;
}
|