Thread: Need help with binary search

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    28

    Need help with binary search

    Hello all, I am currently writing a program that deals with binary searching.To give you some background on the program, a file is opened, loaded into an array.From there, the string the user enters is passed to a function that searches to see if that word exists within the file. Now, I can't seem to get it to work. It's returning false everytime the function executes.here is the code I have

    Code:
    int first=0;
    int last =0;
    word findword;
    last=dictionary.size-1;
    int middle=0;
    int position=-1;
     
    
    findword.found=false;
     
      while(findword.found==false && first<=last)
     {
      middle=(first+last)/2;
      if (strcmp(s,dictionary.words[middle].letters)==0)
      
      
        {
         findword.found=true;
         position=middle;
         return true;
        }
       else if (dictionary.words[middle].letters>s)
       {
         last=middle-1;
       }
       else
       first=middle+1;
       }
       return false;
      }
    Last edited by saldar05; 01-07-2013 at 08:08 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search Tree-search method help?
    By shocklightning in forum C++ Programming
    Replies: 5
    Last Post: 03-25-2012, 10:57 PM
  2. Difference Between A Linear Search And Binary Search
    By ImBack92 in forum C Programming
    Replies: 4
    Last Post: 05-12-2011, 08:47 AM
  3. A Binary Search Tree of... Binary Search Trees...
    By SlyMaelstrom in forum C++ Programming
    Replies: 5
    Last Post: 12-10-2005, 02:12 PM
  4. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 PM
  5. binary search and search using binary tree
    By Micko in forum C++ Programming
    Replies: 9
    Last Post: 03-18-2004, 10:18 AM