Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello I have a problem regarding my c + + assignment Instructions Write a version of the binary search algorithm that can be used to

Hello I have 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 on here:
#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;
}
My rubric(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
My problem is that number 1 and 2 are not checked but the code works. I am new to binary search so I am sorry if this is a hassle. Please take your time and remember to check if its runnable.
I had to resent the problem because my copy and paste didn't put all of it, this should be the code. I am sorry to whoever is going to look at the original question realizing that it is incomplete.

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

Step: 3

blur-text-image

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

Exude confidence, not arrogance.

Answered: 1 week ago

Question

Describe the disciplinary action process.

Answered: 1 week ago