Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Modify the sort function to handle an aray of strngs In the main function dnplay

ID: 3716695 • Letter: M

Question

Modify the sort function to handle an aray of strngs In the main function dnplay the aboh and after, the sort. After the array has been sorted, us8 the bSearch funchion imodfied to searth for s string that the user has entered f fourd drsplay the message Tound along with its oifie t found display the message not found Upload the source cade (cop tWe) thirough quizzes iok an iefh hand se the Blackboard course page Note Your program must be able to handle lowercase and uppercase strings Use the following array string strl f'Mike", 'bob" B, "willim, "George geof

Explanation / Answer

Solution:

code:

#include <iostream>
#include <string>

using namespace std;

int binarySearch(string arr[], string str, int size)
{
    int left = 0;
    int right = size - 1;
    if (left <= right) {
        int mid = (left + right) / 2;
        if (arr[mid] == str) {
            return mid;
        }
        else if (str < arr[mid]) {
            right = mid - 1;
        }
        else {
            left = mid + 1;
        }
    }
    return -1;
}

void bubbleSort(string arr[], int size)
{
    string temp;
    for (int i = 0; i < size; ++i) {
        for (int j = 0; j < size - 1; ++j) {
            if (arr[j] > arr[j + 1]) {
                temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}

void print(string arr[], int size) {
   for(int i = 0; i < size; ++i) {
       cout << arr[i];
       if(size - 1 != i) {
           cout << ", ";
       }
   }
   cout << endl;
}

int main()
{
    string str[] = { "Mike", "bob", "Bill", "william", "George", "geoff" };
    cout << "Array before sorting: ";
    int size = 6;
    print(str, size);
    bubbleSort(str, size);
    cout << "Array before sorting: ";
    print(str, size);
   string temp;
    cout << "Enter a string: ";
    cin >> temp;
    if(binarySearch(str, temp, size) == -1) {
       cout << "not found" << endl;
   } else {
       cout << "found" << endl;
   }
   return 0;
}

I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote