Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4323578
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:01:15+00:00 2026-05-21T09:01:15+00:00

bool binsearch(string phrase, vector<string> words, int from, int to, int &test) { while (tf

  • 0
bool binsearch(string phrase, vector<string> words, int from, int to, int &test)
{
    while (tf == "y") //tf is a global variable
    {
        int mid = (to+from)/2;
        if (words[mid] == phrase) {tf = "t"; return true;}
        if (mid == test) {tf = "f"; return false;}
        if (words[mid] > phrase) {return binsearch(phrase, words, mid-1, to, mid);}
        else {return binsearch(phrase, words, from, mid+1, mid);}
     }
}

i’m working on getting this binary search working. i need the overall function to return either “true” or “false”. i understand how the recursion works up until either line 6 or 7 executes and the return command is invoked. i’ve done research, and it seems like there’s no way to exit the function right there and it has to “unwind” itself. the tf global variable nonsense is so it won’t execute that body again when it’s unwinding…but i’m still not getting the results i want.

essentially, i just want to get out of the function ASAP after either the return true or return false command is invoked, and return the value to the main function accordingly

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T09:01:15+00:00Added an answer on May 21, 2026 at 9:01 am

    I don’t understand your binary search either, and using global variables in addition to recursion leads to programs which are very hard to understand. It’s better to go back the call stack again and “unwind” it properly. Look at the following example (untested):

    bool binsearch(const string& phrase, const vector<string> &words, int from, int to)
    {
        if (from > to)
            return false; // word not found
        int mid = from + (to - from) / 2; // i think your calculation is wrong here
        int cmp = phrase.compare(words[mid]);
        if (cmp < 0)
            return binsearch(phrase, words, from, mid - 1);
        else if (cmp > 0)
            return binsearch(phrase, words, mid + 1, to);
        else
            return true; // word found
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

bool isValid = false; string username = someadmin; If( !String.IsNullOrEmpty(username) && !( username.IndexOf(admin) !=
class Test { bool isVal() const { return isVal; } private: bool isVal; };
public bool AddEntity(int parentId, string description) { try { _connection.Open(); SqlCommand command = new
bool pred(int k, int l, int num1, int num2) { return (num1 < num2);
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { if (textField.text.length >=8) { return NO; //
bool choose() { int answer = 0; while(answer != 1 || answer != 2)
bool Connection::Receive(){ std::vector<uint8_t> buf(1000); boost::asio::async_read(socket_,boost::asio::buffer(buf,1000), boost::bind(&Connection::handler, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); int rcvlen=buf.size(); ByteBuffer b((std::shared_ptr<uint8_t>)buf.data(),rcvlen); if(rcvlen
a bool variable could hold true or false, while bool? could be null as
bool operator()(Iterator it1, Iterator it2) const { return (*it1 < *it2); } Can someone
bool is_something_ok(int param,SomeStruct* p) { bool is_ok = false; // check if is_ok if(is_ok)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.