Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PROGRAM SHOULD BE IN C++ C4.1 Search Benchmarks: The purpose of this program is

ID: 3919524 • Letter: P

Question

PROGRAM SHOULD BE IN C++ C4.1 Search Benchmarks: The purpose of this program is to search for a number using the linear and binary search algorithms and report back how many comparisons were done to find a number using both techniques in a sorted array. Using the “Searching and Sorting Source Code” that I provided last week (posted on Moodle), modify the MAIN source code to prompt the user to enter a number in the range of 1 to 100 to search for. Then, the program should search an array of sorted numbers for the number using both the linearSearch and binarySearch functions. For searching, the program should generate a sorted array of integers from 1 to 100 with 100 elements. You can create a new function called initArray2 (the one I provided in the source code is called initArray and can be used to help write initArray2) to initialize the parameter array as follows: • array [0] = 1 • array [1] = 2 • etc…. • until array [99] = 100 Once the number is found in both the linearSearch and binarySearch functions, the functions should print out how many comparisons were done to find the number before returning to the calling function (main). Input Validation: Do not accept a number to search for that is less than 1 or greater than 100. Remember: • Include several test runs of the program to show successful runs of your program, with both valid and invalid data (to show that you are checking for invalid data) • Please ensure the program is well designed and follows accepted style guidelines (e.g. variable naming, indentation, spacing). • Please ensure the program is well documented, including the overall purpose of the program and documenting all the major sections of the code.

Explanation / Answer

#include<iostream>
#include<conio.h>
int main(){
int a[100],search,count1,count2;
for(int i=1;i<101;i++){
a[i]=i;
}
cout<<"enter a number >0 and <100 to be search ";
cin>>search;
if(search>100 && search<1){
cout<<"invaid entry";
return 0;
}
for(int i=0;i<100;i++){
count1++;
if(a[i]==search){  
break;
}
}
int mid=0,end=100,inicial=0;
for(int i=0;i<end;i++){
mid=a[end/2];
count2++;
if(search>mid){
initial=mid;
end=100;  
}
else if(search<mid){
end=end/2;
}
else if(search==mid){
break;
}
  
}

cout<<"number of comparision:"<<count1+count2;
return 0;
}

note:

i haven't made the inItArray2 function,i have just implemented liner and binary search and put a condition to find the number of comparision done .

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote