Assuming you have sorted the given array write a program to

Assuming you have sorted the given array, write a program to perform a binary search for the element \"pony\".

Pet[0]= \"dog\" Pet[1]= \"cat\" Pet[2]= \"bird\"
Pet[3]= \"snake\" Pet[4]= \"duck\" Pet[5]= \"fish\"
Pet[6]= \"rabbit\" Pet[7]= \"mouse\" Pet[8]= \"pony\"
Pet[9]= \"frog\"

Solution


#include<iostream.h>

#include<string.h>

#include<conio.h>

void main()

{

string pet[10];

int beg,mid,end,i;

String str;

cout << \"\ Enter the 10 strings in sorted order (asc or desc) \ \";

for(i = 0; i < 10;i++) {

cin >> pet[i];

}

beg = 0; end = 9;

cout << \"\ Enter a value to be searched in an array \"; cin >>str;

while( beg <= end) {

mid = (beg+end)/2;

if(strcmp(pet[mid],str)==0) {

cout << \"\ Item found at position \"<< (mid+1); exit(0);

}

else if(strcmp(pet[mid],str)==1)

{

beg=mid+1;

} else if (strcmp(pet[mid],str)==-1)

{

end=mid-1;

}

}

cout << \"Number does not found.\";

}

Assuming you have sorted the given array, write a program to perform a binary search for the element \
Assuming you have sorted the given array, write a program to perform a binary search for the element \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site