- Search Benchmarks
Create a New Project called YourLastnameSearch.
int array[20] = {78, 45, 33, 1, 6, 41, 99, 54, 73, 28, 101, 60, 21, 82, 11, 15, 654, 36, 52, 70};
Write a program that has an array of 20 integers. (You must use the above array.)
It should call a function that uses the linear search algorithm to locate one of the values.
The function should keep a count of the number of comparisons it makes until it finds the value.
The program then should call a function that uses the binary search algorithm to locate the same value.
It should also keep count of the number of the comparisons it makes. Display these values on the screen.
Here is the code that I wrote and it did not work. Please help me find out my error.
using namespace std;
int searchlist (int [], int,int);
int binarysearch (int [], int, int);
const int SIZE = 20;
int main()
#include<iostream>
using namespace std;
int searchlist (int [], int,int);
int binarysearch (int [], int, int);
const int SIZE = 20;
int main()
{
int array[SIZE] = {78, 45, 33, 1, 6, 41, 99, 54, 73, 28, 101, 60, 21, 82, 11, 15, 654, 36, 52, 70};
int resultSearch;
int resultBinary;
int value;
cout<<"Please enter a valid value :";
cin>>value;
cout<< "I will use searchlist to find the value number\n";
resultSearch = searchlist(array, SIZE, value);
if (resultSearch == -1)
cout<<"you did not enter a valid value number \n";
else
{
cout<< "you entered a valid value number \n";
cout<< (resultSearch + 1)<<endl;
}
cout<< " I will use Binary search function to find the value number this time \n";
resultBinary = binarysearch(array, SIZE, value);
if (resultBinary == - 1)
cout<<"you did noy enter a valid value numnber \n";
else
{
cout<<"you entered a value value number \n";
cout<< (resultBinary + 1)<<endl;
}
return 0;
}
int searchList( int list[], int size, int num)
{
int index = 0;
int position = -1;
bool found = false;
while (index < size && !found)
{
if (list[index]== num)
{
found = true;
position = index;
}
index++;
}
return position;
}
int binarysearch (int list[], int size, int num)
{
int first = 0,
last = size - 1,
middle,
position = -1;
bool found = false;
while (!found && first <= last)
{
middle = (first + last) /2;
if (list[middle]==num)
{
found = true;
position = middle;
}
else if (list[middle] > num)
last = middle - 1;
else
first = middle + 1;
}
return position;
}
int array[SIZE] = {78, 45, 33, 1, 6, 41, 99, 54, 73, 28, 101, 60, 21, 82, 11, 15, 654, 36, 52, 70};
int resultSearch;
int resultBinary;
int value;
cout<<"Please enter a valid value :";
cin>>value;
cout<< "I will use searchlist to find the value number\n";
resultSearch = searchlist(array, SIZE, value);
if (resultSearch == -1)
cout<<"you did not enter a valid value number \n";
else
{
cout<< "you entered a valid value number \n";
cout<< (resultSearch + 1)<<endl;
}
cout<< " I will use Binary search function to find the value number this time \n";
resultBinary = binarysearch(array, SIZE, value);
if (resultBinary == - 1)
cout<<"you did noy enter a valid value numnber \n";
else
{
cout<<"you entered a value value number \n";
cout<< (resultBinary + 1)<<endl;
}
return 0;
#include<iostream>
using namespace std;
int searchlist (int [], int,int);
int binarysearch (int [], int, int);
const int SIZE = 20;
int main()
{
int array[SIZE] = {78, 45, 33, 1, 6, 41, 99, 54, 73, 28, 101, 60, 21, 82, 11, 15, 654, 36, 52, 70};
int resultSearch;
int resultBinary;
int value;
cout<<"Please enter a valid value :";
cin>>value;
cout<< "I will use searchlist to find the value number\n";
resultSearch = searchlist(array, SIZE, value);
if (resultSearch == -1)
cout<<"you did not enter a valid value number \n";
else
{
cout<< "you entered a valid value number \n";
cout<< (resultSearch + 1)<<endl;
}
cout<< " I will use Binary search function to find the value number this time \n";
resultBinary = binarysearch(array, SIZE, value);
if (resultBinary == - 1)
cout<<"you did noy enter a valid value numnber \n";
else
{
cout<<"you entered a value value number \n";
cout<< (resultBinary + 1)<<endl;
}
return 0;
}
int searchList( int list[], int size, int num)
{
int index = 0;
int position = -1;
bool found = false;
while (index < size && !found)
{
if (list[index]== num)
{
found = true;
position = index;
}
index++;
}
return position;
}
int binarysearch (int list[], int size, int num)
{
int first = 0,
last = size - 1,
middle,
position = -1;
bool found = false;
while (!found && first <= last)
{
middle = (first + last) /2;
if (list[middle]==num)
{
found = true;
position = middle;
}
else if (list[middle] > num)
last = middle - 1;
else
first = middle + 1;
}
return position;
}
int searchList( int list[], int size, int num)
{
#include<iostream>
using namespace std;
int searchlist (int [], int,int);
int binarysearch (int [], int, int);
const int SIZE = 20;
int main()
{
int array[SIZE] = {78, 45, 33, 1, 6, 41, 99, 54, 73, 28, 101, 60, 21, 82, 11, 15, 654, 36, 52, 70};
int resultSearch;
int resultBinary;
int value;
cout<<"Please enter a valid value :";
cin>>value;
cout<< "I will use searchlist to find the value number\n";
resultSearch = searchlist(array, SIZE, value);
if (resultSearch == -1)
cout<<"you did not enter a valid value number \n";
else
{
cout<< "you entered a valid value number \n";
cout<< (resultSearch + 1)<<endl;
}
cout<< " I will use Binary search function to find the value number this time \n";
resultBinary = binarysearch(array, SIZE, value);
if (resultBinary == - 1)
cout<<"you did noy enter a valid value numnber \n";
else
{
cout<<"you entered a value value number \n";
cout<< (resultBinary + 1)<<endl;
}
return 0;
}
int searchList( int list[], int size, int num)
{
int index = 0;
int position = -1;
bool found = false;
while (index < size && !found)
{
if (list[index]== num)
{
found = true;
position = index;
}
index++;
}
return position;
}
int binarysearch (int list[], int size, int num)
{
int first = 0,
last = size - 1,
middle,
position = -1;
bool found = false;
while (!found && first <= last)
{
middle = (first + last) /2;
if (list[middle]==num)
{
found = true;
position = middle;
}
else if (list[middle] > num)
last = middle - 1;
else
first = middle + 1;
}
return position;
}
#include<iostream>
using namespace std;
int searchlist (int [], int,int);
int binarysearch (int [], int, int);
const int SIZE = 20;
int main()
{
int array[SIZE] = {78, 45, 33, 1, 6, 41, 99, 54, 73, 28, 101, 60, 21, 82, 11, 15, 654, 36, 52, 70};
int resultSearch;
int resultBinary;
int value;
cout<<"Please enter a valid value :";
cin>>value;
cout<< "I will use searchlist to find the value number\n";
resultSearch = searchlist(array, SIZE, value);
if (resultSearch == -1)
cout<<"you did not enter a valid value number \n";
else
{
cout<< "you entered a valid value number \n";
cout<< (resultSearch + 1)<<endl;
}
cout<< " I will use Binary search function to find the value number this time \n";
resultBinary = binarysearch(array, SIZE, value);
if (resultBinary == - 1)
cout<<"you did noy enter a valid value numnber \n";
else
{
cout<<"you entered a value value number \n";
cout<< (resultBinary + 1)<<endl;
}
return 0;
}
int searchList( int list[], int size, int num)
{
int index = 0;
int position = -1;
bool found = false;
while (index < size && !found)
{
if (list[index]== num)
{
found = true;
position = index;
}
index++;
}
return position;
}
int binarysearch (int list[], int size, int num)
{
int first = 0,
last = size - 1,
middle,
position = -1;
bool found = false;
while (!found && first <= last)
{
middle = (first + last) /2;
if (list[middle]==num)
{
found = true;
position = middle;
}
else if (list[middle] > num)
last = middle - 1;
else
first = middle + 1;
}
return position;
}
int index = 0;
int position = -1;
bool found = false;
while (index < size && !found)
{
if (list[index]== num)
{
found = true;
position = index;
}
index++;
} return position;
st + last) /2;
if (list[middle]==num)
{
found = true;
position = middle;
}
else if (list[middle] > num)
last = middle - 1;
else
first = middle + 1;
}
return position;
}