Answered step by step
Verified Expert Solution
Link Copied!

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 8 to sort the list.) Write a program to test the function and prompt the user to enter 10 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 binarySearch(const std::vector& arr, const std::string& key){
int left =0;
int right = arr.size()-1;
while (left <= right){
int mid = left +(right - left)/2;
if (arr[mid].str == key){
return mid; // Key found, return its position in the sorted array
}
if (arr[mid].str < key){
left = mid +1;
} else {
right = mid -1;
}
}
return -1; // Key not found
}
// Selection sort function for strings with positions
void selectionSort(std::vector& arr){
int n = arr.size();
for (int i =0; i < n -1; ++i){
int minIdx = i;
for (int j = i +1; j < n; ++j){
if (arr[j].str < arr[minIdx].str){
minIdx = j;
}
}
std::swap(arr[i], arr[minIdx]);
}
}
int main(){
std::vector strList(10);
std::string input;
// Input 10 strings from the user
for (int i =0; i <10; ++i){
std::cout << "Enter string "<<(i +1)<<": ";
std::cin >> input;
strList[i]={input, i +1};
}
// Sort the list of strings
selectionSort(strList);
// 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 = binarySearch(strList, input);
if (result !=-1){
std::cout << input <<" is found at position "<< strList[result].originalPos << std::endl;
} else {
std::cout << input <<" is not in the list." << std::endl;
}
return 0;
}
The rubric, (my problem)
Status: FAILED!
Check: 1
Test: Binary search on an ordered list
Reason: Unable to find '['b is found at position 1']' in the program's output.
Enter string 1: Enter string 2: Enter string 3: Enter string 4: Enter string 5: Enter string 6: Enter string 7: Enter string 8: Enter string 9: Enter string 10: Enter a string to search for: b is found at position 2
.
Error : AssertionError - Unable to find b is found at position 1 in the program's output.
Timestamp: 2024-06-0113:59:07.031576
Status: FAILED!
Check: 2
Test: Binary search on an unordered list
Reason: Unable to find '['c is found at position 1']' in the program's output.
Enter string 1: Enter string 2: Enter string 3: Enter string 4: Enter string 5: Enter string 6: Enter string 7: Enter string 8: Enter string 9: Enter string 10: Enter a string to search for: c is found at position 4
.
Error : AssertionError - Unable to find c is found at position 1 in the program's output.
Timestamp: 2024-06-0113:59:07.033832
Status: PASSED!
Check: 3
Test: Searching for a missing item
Reason: None
Timestamp: 2024-06-0113:59:07.036173
As you can see the code passed the 3rd check but doesn't seem to be checking the 1st and 2nd, 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Server T-SQL Recipes

Authors: David Dye, Jason Brimhall

4th Edition

1484200616, 9781484200612

More Books

Students also viewed these Databases questions

Question

Explain the significance of employee selection.

Answered: 1 week ago

Question

Discuss the performance appraisal process.

Answered: 1 week ago