Question

************************CODE SHOULD BE WRITTEN IN C++*************************** TASK: "Given a sorted array of String and a String...

************************CODE SHOULD BE WRITTEN IN C++***************************

TASK:

"Given a sorted array of String and a String x, find index of x if it is present in the array."

*************************USE THE FUNCTION***********************************************************************

#include "problem2.h"

// Return index of x if it is present in arr[],
// else return -1
int binarySearch(char* arr[], char* x, int n)
{
//WRITTEN CODE

return -1;
}

******************************EXAMPLE******************************************************************************

Input :  arr[] = { "contribute", "geeks", "ide", "practice"}, x = "ide"
Output :  2
The String x is present at index 2.

Input :  arr[] = { "contribute", "geeks", "ide", "practice"}, x = "zz"
Output :  -1
The String "zz" is not present.
0 0
Add a comment Improve this question Transcribed image text
Answer #1
int binarySearch(char* arr[], char* x, int n)
{
    int left = 0, right = n-1, mid;
    while(left <= right) {
        mid = (left + right) / 2;
        if(strcmp(arr[mid], x) == 0) {
            return mid;
        } else if(strcmp(arr[mid], x) > 0) {
            right = mid-1;
        } else {
            left = mid+1;
        }
    }
    return -1;
}
Add a comment
Know the answer?
Add Answer to:
************************CODE SHOULD BE WRITTEN IN C++*************************** TASK: "Given a sorted array of String and a String...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • IN JAVA please Given a sorted array and a target value, return the index if the...

    IN JAVA please Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. Your code will be tested for runtime. Code which does not output a result in logarithmic time (making roughly log(2) N comparisons) will fail the tests. A sample main function is provided so that you may test your code on sample inputs. For testing purposes, the...

  • In C++, make the following binary search function work on an array of strings to give...

    In C++, make the following binary search function work on an array of strings to give you the index location of the string you want in the array. It currently works on integers only. int binarySearch(int arr[], int firstIndex, int lastIndex, int target){ int index; if (firstIndex>lastIndex) index = -1; else { int mid = firstIndex + (lastIndex - firstIndex) / 2; if (target == arr[mid]) index = mid; else if (target < arr[mid]) index = binarySearch(arr, firstIndex, mid -...

  • 1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array...

    1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array of distinct integers, and an integer x, return the index of x in the array. If the element x is not present in the array, return -1. "Almost sorted" means the following. Assume you had a sorted array A[0…N], and then split it into two pieces A[0…M] and A[M+1…N], and move the second piece upfront to get the following: A[M+1]…A[N]A[0]…A[M]. Thus, the "almost sorted"...

  • I'm trying to do an insertion sort in which i sort the characters of a string,...

    I'm trying to do an insertion sort in which i sort the characters of a string, what do i replace inputString.charAt(j) with? note: //arr[j] = arr[j-1] is an example that my professor used for an array. (just a reminder for myself) Written in Java lang public static String sort(String inputString) if (inputS tring null) t //insertion sortsort characters for(int i = 0; i inputstring.length(); i++) int index - inputString.charAt(i); int j- i; while( i> 0 && inputString.charAt (i-1) > index)...

  • Given the contents of a sorted array of size 6, shown below, show which indices of...

    Given the contents of a sorted array of size 6, shown below, show which indices of the array will be visited, and in what order, if the algorithm BinarySearch was to be performed on this array for the number 4. int theArray[] = {1, 2, 3, 6, 8, 9}; Following are example question & answer: Given the contents of a sorted array of size 7, shown below, show which indices of the array will be visited, and in what order,...

  • Transfer C code of selection sort to MIPS code and print the sorted array/results data Array:...

    Transfer C code of selection sort to MIPS code and print the sorted array/results data Array: word 43, -5, 11, 12, 64, -7, 14, 71, 70, 13, -27 string: asciz"In" # Trantec the C code of selection sort to MIPS code. Do not modify the existing code and structure! text main la ŞtO, Array li $t1, 0 li $t7,11 mul $17, $17, 4 subi $t8,$t7, 4 # array length n-11 # 4*n #4*(n-1) # lis in $t1 and j is...

  • Transfer C code of selection sort to MIPS code and print the sorted array/results

    Transfer C code of selection sort to MIPS code and print the sorted array/results data Array: word 43, -5, 11, 12, 64, -7, 14, 71, 70, 13, -27 string: asciz"In" # Trantec the C code of selection sort to MIPS code. Do not modify the existing code and structure! text main la ŞtO, Array li $t1, 0 li $t7,11 mul $17, $17, 4 subi $t8,$t7, 4 # array length n-11 # 4*n #4*(n-1) # lis in $t1 and j is...

  • You will be reading in 3 files in the program. One will contain a list of...

    You will be reading in 3 files in the program. One will contain a list of 1000 words in unsorted order. The second file will contain 1000 words in sorted order. The final file will contain 20 words to be searched for. The main program has been written for you. You will be implementing three functions: bool readWords(string array[], int size, string fileName); int linearSearch(string wordToFind, const string words[], int size); int binarySearch(string wordToFind, const string words[], int size); The...

  • in c++ please program for this code #include <iostream> #include <fstream> #include <string> #include <cstring> //...

    in c++ please program for this code #include <iostream> #include <fstream> #include <string> #include <cstring> // for string tokenizer and c-style string processing #include <algorithm> // max function #include <stdlib.h> #include <time.h> using namespace std; // Extend the code here as needed class BTNode{ private: int nodeid; int data; int levelNum; BTNode* leftChildPtr; BTNode* rightChildPtr; public: BTNode(){} void setNodeId(int id){ nodeid = id; } int getNodeId(){ return nodeid; } void setData(int d){ data = d; } int getData(){ return data;...

  • Teacher Instructions: Create the binarySearch() method for String arrays from the downloaded version for ints so...

    Teacher Instructions: Create the binarySearch() method for String arrays from the downloaded version for ints so the program can find String. Wrap a main around it, of course, so that we can see how it works. Provided Code: // Returns the index of an occurrence of target in a, // or a negative number if the target is not found. // Precondition: elements of a are in sorted order public static int binarySearch(int[] a, int target) { int min =...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT