Advertisement
rinab333

CSCI 340 assign 2

Jul 16th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.51 KB | None | 0 0
  1. //terina burr
  2. //section one
  3. //assign two
  4. //due 10/11/15
  5. #include <iostream>
  6. #include <vector>
  7. #include <cstdlib>
  8. #include <algorithm>
  9. #include <iomanip>
  10. using namespace std;
  11.  
  12. const int TOBE_SEARCHED = 5000;
  13. const int HIGH = 10000;
  14. const int LOW = 1;
  15. void genRndNums( vector<int>& v, int vec_size, int seed );
  16.  
  17. bool linearSearch( const vector<int>& v, int x) ;
  18.  
  19. bool binarySearch( const vector<int>& v, int x) ;
  20. int search( const vector<int>& container, const vector<int>& searchNums,
  21.             bool (*p)( const vector<int>&, int) ) ;
  22. void sortVec (vector<int>& v) ;
  23.  
  24. void printStat (int totalSucCnt, int vec_size);
  25.  
  26.  
  27. int main() {
  28.     vector<int> container, tobeSearched;
  29.     genRndNums(container, 10000, 9);
  30.     genRndNums(tobeSearched, TOBE_SEARCHED, 3);
  31.  
  32.     cout << "\nConducting linear search ..." << endl;
  33.     int linear_search_count = search( container, tobeSearched, linearSearch );
  34.     printStat ( linear_search_count, TOBE_SEARCHED );
  35.  
  36.     cout << "\nConducting binary search on unsorted container ..." << endl;
  37.     int binary_search_count = search( container, tobeSearched, binarySearch );
  38.     printStat ( binary_search_count, TOBE_SEARCHED );
  39.  
  40.     sortVec( container );
  41.  
  42.     cout << "\nConducting binary search on sorted container ..." << endl;
  43.     binary_search_count = search( container, tobeSearched, binarySearch );
  44.     printStat ( binary_search_count, TOBE_SEARCHED );
  45.  
  46.     return 0;
  47. }
  48.  
  49.  
  50.  
  51. /***************************************************************
  52. Function: genRndNums
  53.  
  54. Use:      it generates random number
  55.  
  56. Arguments: 1. V: the vector that it puts the random numbers in
  57.            2. vec_size: the size of the vector you want to have
  58.            3. seed:
  59.  
  60. Returns:   nothing
  61. ***************************************************************/
  62.  
  63. void genRndNums( vector<int>& v, int vec_size, int seed ) {
  64.  
  65.         srand(seed);
  66.         int x;
  67.         for(int i = 0; i<vec_size; i++)
  68. //this loop goes through a vector and inserts the random numbers        
  69.     {
  70.                 x = rand() % HIGH + LOW;
  71.                 v.push_back(x);
  72.         }
  73.  
  74. }
  75.  
  76. /***************************************************************
  77. Function: linearSearch
  78.  
  79. Use:      it starts searching for x at the beginning of the vector v
  80.  
  81. Arguments: 1. V: the vector that it searches
  82.            2. x: is the item that it searches
  83.  
  84.  
  85. 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
  86. ***************************************************************/
  87.  
  88.  
  89. bool linearSearch( const vector<int>& v, int x) {
  90.     if(std::find(v.begin(), v.end(), x) !=v. end())
  91.     {
  92. //if they find the correct value and it is not equal to the last one it returns true
  93.         return true;
  94.     }
  95.  
  96.     else
  97.         return false;
  98. //otherwise it returns false
  99. }
  100.  
  101. /***************************************************************
  102. Function: binarySearch
  103.  
  104. Use:      it uses the algorithm binary search to find the value x in the array v
  105.  
  106. Arguments: 1. V: the vector that it searches
  107.            2. x: is the item that it searches
  108.  
  109.  
  110. 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
  111. ***************************************************************/
  112.  
  113.  
  114.  
  115. bool binarySearch( const vector<int>& v, int x) {
  116.     if ( binary_search( v.begin(), v.end(), x ) )
  117.         return true;
  118. //if they find the correct value return true
  119.     else
  120.         return false;
  121. //else return false
  122. }
  123. /***************************************************************
  124. Function: search
  125.  
  126. Use:    it is used to compute the total number of successful searches
  127. Arguments: 1. container: the vector that it searches from
  128.            2. searchNums: the numbers its suppose to search
  129.            3. p: a pointer to a search routine
  130.  
  131.  
  132. Returns:   the total number of successful searches
  133. ***************************************************************/
  134.  
  135. int search( const vector<int>& container, const vector<int>& searchNums,
  136.             bool (*p)( const vector<int>&, int) ) {
  137. int x = 0;
  138.     for(unsigned int i = 0; i<searchNums.size(); i++)
  139.     {
  140. //goes through the second vector and stops when it reaches its size
  141.         if(p(container,searchNums.at(i))==true)
  142.         {
  143. //it points to a search function and if that search function goes through as true then it adds one to the total it says going through vector one
  144.             x++;
  145.         }
  146.     }
  147.     return x;
  148.  
  149. }
  150. /***************************************************************
  151. Function: sortVec
  152.  
  153. Use:    it is used to sort a vector from smallest to largest using the algorithm sort
  154. Arguments: 1. v: the vector that it is going to sort
  155.  
  156.  
  157. Returns:   nothing
  158. ***************************************************************/
  159.  
  160.  
  161. void sortVec (vector<int>& v) {
  162. //sorts a vector from smallest to largest
  163.     sort(v.begin(), v.end() );
  164. }
  165. /***************************************************************
  166. Function: printStat
  167.  
  168. Use:    it is used to print out the percentage of successful searches
  169. Arguments: 1. totalSucCnt: total number of successful searches
  170.            2. vec_size: the size of the vector
  171.  
  172. Returns:   nothing
  173. ***************************************************************/
  174.  
  175.  
  176. void printStat (int totalSucCnt, int vec_size) {
  177.     float x =( float(totalSucCnt)/vec_size)*100;
  178. //gives the percentage number of successes as a float
  179.     std::cout.width(6); std::cout << std::right <<"Percent of successful searches = "<<setw(4)<< x<<"%"<<'\n';
  180.  
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement