77.2k views
4 votes
Rewrite the Demetris Leadership Center (DLC) program (a.k.a Program 8-3) so that two changes are made: • A Linear search is done, rather than a binary search. • Vectors are used instead of arrays. • Both Programs 8-3, which uses the binary search and 8-1, which uses a linear search, have been included so you can see what changes need to be made. • When you have the modified Program 8-3 working, call the instructor over so you may demonstrate your program.

User Broncha
by
4.7k points

1 Answer

5 votes

Answer:

See explaination

Step-by-step explanation:

The program code

#include <iostream>

#include <vector>

#include <algorithm>

#include <cstdlib>

#include <iomanip>

using namespace std;

const int DATA_SIZE = 100;

const int SEARCH_SIZE = 50;

const int DATA_SEED = 11;

const int SEARCH_SEED = 7;

void genRndNums( vector<int>& v, int seed ) {

srand(seed);

for(int i=0;i<v.size();++i)

v[i] = rand() % ( 100) + 1.;

}

bool linearSearch( const vector<int>& inputVec, int x) {

for(int i=0;i<inputVec.size(); ++i){

if( inputVec[i] ==x)

return true;

}

return false;

}

bool binarySearch( const vector<int>& inputVec, int x) {

int low = 0, high = inputVec.size()-1;

while (low <= high)

{

int mid = low + (high-low)/2;

if (inputVec[mid] == x)

return true;

if (inputVec[mid] < x)

low = mid + 1;

else

high = mid - 1;

}

return false;

}

int search( const vector<int>& inputVec, const vector<int>& searchVec,

bool (*p)( const vector<int>&, int) ){

int count = 0;

for(int i=0;i<searchVec.size(); ++i){

if( p(inputVec, searchVec[i])){

++count;

}

}

return count;

}

void sortVector (vector<int>& inputVec) {

std::sort(inputVec.begin(),inputVec.end() );

}

void printStat (int totalSucCnt, int vec_size) {

cout<<"Total SuccessCount: "<<totalSucCnt<<endl;

}

void print_vec( const vector<int>& vec ){

for(int i=0;i<vec.size();++i)

cout<< vec[i] <<" ";

cout<<endl;

}

int main() {

vector<int> inputVec(DATA_SIZE);

vector<int> searchVec(SEARCH_SIZE);

genRndNums(inputVec, DATA_SEED);

genRndNums(searchVec, SEARCH_SEED);

cout << "----- Data source: " << inputVec.size() << " randomly generated numbers ------" << endl;

print_vec( inputVec );

cout << "----- " << searchVec.size() << " random numbers to be searched -------" << endl;

print_vec( searchVec );

cout << "\\Conducting linear search on unsorted data source ..." << endl;

int linear_search_count = search( inputVec, searchVec, linearSearch );

printStat ( linear_search_count, SEARCH_SIZE );

cout << "\\Conducting binary search on unsorted data source ..." << endl;

int binary_search_count = search( inputVec, searchVec, binarySearch );

printStat ( binary_search_count, SEARCH_SIZE );

sortVector( inputVec );

cout << "\\Conducting linear search on sorted data source ..." << endl;

linear_search_count = search( inputVec, searchVec, linearSearch );

printStat ( linear_search_count, SEARCH_SIZE );

cout << "\\Conducting binary search on sorted data source ..." << endl;

binary_search_count = search( inputVec, searchVec, binarySearch );

printStat ( binary_search_count, SEARCH_SIZE );

return 0;

}

Check attachment for source code and output.

Rewrite the Demetris Leadership Center (DLC) program (a.k.a Program 8-3) so that two-example-1
User Msigman
by
5.1k points