1. Search Benchmarks int array[20] = {78, 45, 33, 1, 6, 41, 99, 54, 73, 28, 101,
ID: 3542568 • Letter: 1
Question
1. Search Benchmarks
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.
2. Sorting Orders
int first_array[8] = {13, 9, 36, 47, 3, 88, 7, 100};
int second_array[8] = {13, 9, 36, 47, 3, 88, 7, 100};
Write a program that uses two identical arrays of just eight integers. (You must use the above arrays.) It should display the contents of the first array, then call a function to sort the array using an ascending order bubble sort modified to print out the array contents after each pass of the sort. Next, the program should display the contents of the second array, then call a function to sort the array using an ascending order selection sort modified to print out the array contents after each pass of the sort.
3. Write a program that uses a structure to store the following weather data for a particular month:
Total Rainfall
High Temperature
Low Temperature
Average Temperature
The program should have an array of 12 structures to hold weather data for an entire year. When the program runs, it should ask the user to enter data for each month. (The average temperature should be calculated be calculated, using the high and low temperatures.) Once the data is entered for all the months, the program should calculate and display the average monthly rainfall, the total rainfall for the year, the highest and lowest temperatures for the year (and the months they occurred in), and the average of all the monthly average temperatures.
Input Validation: Only accept temperatures within the range between -100 and +140 degrees Fahrenheit.
Explanation / Answer
#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 ";
resultSearch = searchlist(array, SIZE, value);
if (resultSearch == -1)
cout<<"you did not enter a valid value number ";
else
{
cout<< "you entered a valid value number ";
cout<< (resultSearch + 1)<<endl;
}
cout<< " I will use Binary search function to find the value number this time ";
resultBinary = binarysearch(array, SIZE, value);
if (resultBinary == - 1)
cout<<"you did noy enter a valid value numnber ";
else
{
cout<<"you entered a value value number ";
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 ";
resultSearch = searchlist(array, SIZE, value);
if (resultSearch == -1)
cout<<"you did not enter a valid value number ";
else
{
cout<< "you entered a valid value number ";
cout<< (resultSearch + 1)<<endl;
}
cout<< " I will use Binary search function to find the value number this time ";
resultBinary = binarysearch(array, SIZE, value);
if (resultBinary == - 1)
cout<<"you did noy enter a valid value numnber ";
else
{
cout<<"you entered a value value number ";
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 ";
resultSearch = searchlist(array, SIZE, value);
if (resultSearch == -1)
cout<<"you did not enter a valid value number ";
else
{
cout<< "you entered a valid value number ";
cout<< (resultSearch + 1)<<endl;
}
cout<< " I will use Binary search function to find the value number this time ";
resultBinary = binarysearch(array, SIZE, value);
if (resultBinary == - 1)
cout<<"you did noy enter a valid value numnber ";
else
{
cout<<"you entered a value value number ";
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 ";
resultSearch = searchlist(array, SIZE, value);
if (resultSearch == -1)
cout<<"you did not enter a valid value number ";
else
{
cout<< "you entered a valid value number ";
cout<< (resultSearch + 1)<<endl;
}
cout<< " I will use Binary search function to find the value number this time ";
resultBinary = binarysearch(array, SIZE, value);
if (resultBinary == - 1)
cout<<"you did noy enter a valid value numnber ";
else
{
cout<<"you entered a value value number ";
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 ";
resultSearch = searchlist(array, SIZE, value);
if (resultSearch == -1)
cout<<"you did not enter a valid value number ";
else
{
cout<< "you entered a valid value number ";
cout<< (resultSearch + 1)<<endl;
}
cout<< " I will use Binary search function to find the value number this time ";
resultBinary = binarysearch(array, SIZE, value);
if (resultBinary == - 1)
cout<<"you did noy enter a valid value numnber ";
else
{
cout<<"you entered a value value number ";
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;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.