Question: Hello, I am having a problem with my c + + assignment and wondering what I can do to fix it Instructions are: Write a
Hello, I am having a problem with my c assignment and wondering what I can do to fix it
Instructions are:
Write a version of the binary search algorithm that can be used to search a list of strings. Use the selection sort that you designed in Exercise to sort the list. Write a program to test the function and prompt the user to enter strings. The program should output the position of the string if found in the list and the following message if not:
x is not in the list
What I got so far:
#include
#include
#include
Binary search function for integers
int binarySearchconst std::vector& arr, int key
int left ;
int right arr.size;
while left right
int mid left right left;
if arrmid key
return mid; Key found, return its position
if arrmid key
left mid ;
else
right mid ;
return ; Key not found
int main
std::vector intList;
int key;
Input integers from the user
for int i ; i ; i
std::cout "Enter integer i : ;
std::cin key;
intList.pushbackkey;
Sort the list of integers
std::sortintListbegin intList.end;
Prompt the user to enter an integer to search for
std::cout "Enter an integer to search for: ;
std::cin key;
Perform binary search
int result binarySearchintList key;
if result
std::cout key is found at position result std::endl;
else
std::cout key is not in the list." std::endl;
return ;
My outputthe problem
Enter integer :
Enter integer :
Enter integer :
Enter integer :
Enter integer :
Enter integer :
Enter integer :
Enter integer :
Enter integer :
Enter integer :
Enter an integer to search for:
is found at position
My problem is that should be on position but says its position Its late at night so I am tired atm. Please make sure yours is runnable. ty
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
