//terina burr //section one //assign two //due 10/11/15 #include #include #include #include #include using namespace std; const int TOBE_SEARCHED = 5000; const int HIGH = 10000; const int LOW = 1; void genRndNums( vector& v, int vec_size, int seed ); bool linearSearch( const vector& v, int x) ; bool binarySearch( const vector& v, int x) ; int search( const vector& container, const vector& searchNums, bool (*p)( const vector&, int) ) ; void sortVec (vector& v) ; void printStat (int totalSucCnt, int vec_size); int main() { vector container, tobeSearched; genRndNums(container, 10000, 9); genRndNums(tobeSearched, TOBE_SEARCHED, 3); cout << "\nConducting linear search ..." << endl; int linear_search_count = search( container, tobeSearched, linearSearch ); printStat ( linear_search_count, TOBE_SEARCHED ); cout << "\nConducting binary search on unsorted container ..." << endl; int binary_search_count = search( container, tobeSearched, binarySearch ); printStat ( binary_search_count, TOBE_SEARCHED ); sortVec( container ); cout << "\nConducting binary search on sorted container ..." << endl; binary_search_count = search( container, tobeSearched, binarySearch ); printStat ( binary_search_count, TOBE_SEARCHED ); return 0; } /*************************************************************** Function: genRndNums Use: it generates random number Arguments: 1. V: the vector that it puts the random numbers in 2. vec_size: the size of the vector you want to have 3. seed: Returns: nothing ***************************************************************/ void genRndNums( vector& v, int vec_size, int seed ) { srand(seed); int x; for(int i = 0; i& v, int x) { if(std::find(v.begin(), v.end(), x) !=v. end()) { //if they find the correct value and it is not equal to the last one it returns true return true; } else return false; //otherwise it returns false } /*************************************************************** Function: binarySearch Use: it uses the algorithm binary search to find the value x in the array v Arguments: 1. V: the vector that it searches 2. x: is the item that it searches Returns: true or false whether the item x is in the vector v or not returns true if the item x is in the vector v returns false if the item x is not in the vector v ***************************************************************/ bool binarySearch( const vector& v, int x) { if ( binary_search( v.begin(), v.end(), x ) ) return true; //if they find the correct value return true else return false; //else return false } /*************************************************************** Function: search Use: it is used to compute the total number of successful searches Arguments: 1. container: the vector that it searches from 2. searchNums: the numbers its suppose to search 3. p: a pointer to a search routine Returns: the total number of successful searches ***************************************************************/ int search( const vector& container, const vector& searchNums, bool (*p)( const vector&, int) ) { int x = 0; for(unsigned int i = 0; i& v) { //sorts a vector from smallest to largest sort(v.begin(), v.end() ); } /*************************************************************** Function: printStat Use: it is used to print out the percentage of successful searches Arguments: 1. totalSucCnt: total number of successful searches 2. vec_size: the size of the vector Returns: nothing ***************************************************************/ void printStat (int totalSucCnt, int vec_size) { float x =( float(totalSucCnt)/vec_size)*100; //gives the percentage number of successes as a float std::cout.width(6); std::cout << std::right <<"Percent of successful searches = "<