Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I am having a problem regarding my c + + assignment instructions: Write a version of the binary search algorithm that can be used to
I am having a problem regarding my c assignment
instructions:
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 wrote with the help of someone one this website:
#include
#include
#include
#include
Struct to store strings with their original positions
struct StringWithPosition
std::string str;
int originalPos;
;
Binary search function for strings
int binarySearchconst std::vector& arr, const std::string& key
int left ;
int right arr.size;
while left right
int mid left right left;
if arrmidstr key
return mid; Key found, return its position in the sorted array
if arrmidstr key
left mid ;
else
right mid ;
return ; Key not found
Selection sort function for strings with positions
void selectionSortstd::vector& arr
int n arr.size;
for int i ; i n ; i
int minIdx i;
for int j i ; j n; j
if arrjstr arrminIdxstr
minIdx j;
std::swaparri arrminIdx;
int main
std::vector strList;
std::string input;
Input strings from the user
for int i ; i ; i
std::cout "Enter string i : ;
std::cin input;
strListiinput i ;
Sort the list of strings
selectionSortstrList;
Prompt the user to enter a string to search for
std::cout "Enter a string to search for: ;
std::cin input;
Perform binary search
int result binarySearchstrList input;
if result
std::cout input is found at position strListresultoriginalPos std::endl;
else
std::cout input is not in the list." std::endl;
return ;
The rubric, my problem
Status: FAILED!
Check:
Test: Binary search on an ordered list
Reason: Unable to find b is found at position in the program's output.
Enter string : Enter string : Enter string : Enter string : Enter string : Enter string : Enter string : Enter string : Enter string : Enter string : Enter a string to search for: b is found at position
Error : AssertionError Unable to find b is found at position in the program's output.
Timestamp: ::
Status: FAILED!
Check:
Test: Binary search on an unordered list
Reason: Unable to find c is found at position in the program's output.
Enter string : Enter string : Enter string : Enter string : Enter string : Enter string : Enter string : Enter string : Enter string : Enter string : Enter a string to search for: c is found at position
Error : AssertionError Unable to find c is found at position in the program's output.
Timestamp: ::
Status: PASSED!
Check:
Test: Searching for a missing item
Reason: None
Timestamp: ::
As you can see the code passed the rd check but doesn't seem to be checking the st and nd I am relatively new to binary code so I am sorry this looks like a hassle to do Take your time and remember to check if the code is runnable. Ty
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started