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

C++ PROGRAMMING For this week’s assignment, you’ll need to make two functions, o

ID: 3799918 • Letter: C

Question

C++ PROGRAMMING

For this week’s assignment, you’ll need to make two functions, one which performs a linear search, and one which performs a binary search. These searches will be performed on the following three arrays:

Array A: { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

Array B: {1, 2, 3, 5, 8, 13, 21, 34, 55, 89}

Array C: {20, 18, 16, 14, 12, 10, 8, 6, 4, 2}

FOR YOUR ASSIGNMENT: Search all three arrays following values using BOTH the linear and binary searches, report if they’re located or not, and indicate how many checks you did before you discovered your answer for each.

Value 1: Search for 5

Value 2: Search for 4

Explanation / Answer

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
using namespace std;


void linearsearch(int arr[],int key)
{
int i;
int count=0;
int noofchecks;
for(i=0;i<10;i++)
{
   ++noofchecks;
   if(arr[i]==key)
   {
       ++count;
       cout<<"element is found :"<<key<<" at the pos :"<<i<<endl;
       break;
   }
  
}
  
if(count==0)
{
   cout<<"element not found"<<endl;
}
cout<<"the no of checks for finding element in the array in the linear search is :"<<noofchecks<<endl;
  
}
void binarySearch(int arr[],int key,int size)
{


int starting=1, ending=size;
int middle=(starting+ending)/2;
int noofchecks=0;

while(starting<=ending&&arr[middle]!=key){
   ++noofchecks;
if(arr[middle]<key){
starting=middle+1;
}
else{
ending=middle-1;
}
middle=(starting+ending)/2;
}// While Loop End

if(arr[middle]==key)
{

cout<<"element is fount at :"<<middle<<endl;
cout<<"the no of checks for finding element in the array in the binarysearch is :"<<noofchecks<<endl;

}
else
cout<<"element not found "<<endl;
cout<<"the no of checks for finding element in the array in the binarysearch is :"<<noofchecks<<endl;


}
int main()
{
   int num;
   int array[]={ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
   cout<<"enter the key element to search in the arrya"<<endl;
  
   cin>>num;
   linearsearch(array,num);
  
   binarySearch(array,num,10);
   return 0;
}
  
  

output

enter the key element to search in the arrya
15
element not found
the no of checks for finding element in the array in the linear search is :10
element not found
the no of checks for finding element in the array in the binary search is :4

--------------------------------
Process exited after 4.848 seconds with return value 0
Press any key to continue . . .

enter the key element to search in the arrya
5
element is found :5 at the pos :4
the no of checks for finding element in the array in the linear search is :5
element is fount at :4
the no of checks for finding element in the array in the binary search is :3

--------------------------------
Process exited after 2.26 seconds with return value 0
Press any key to continue . . .

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