1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

23 HOURS WASTED! Due tonight need help

Discussion in 'C' started by crashcoder, Apr 25, 2012.

  1. crashcoder

    crashcoder New Member

    Joined:
    Apr 25, 2012
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    florida
    Hi there, I spent nearly two days worth of time trying to complete a program which is due 4/25 at midnight. The program asks the programmer to create a program that does the following:
    * reads in 250 words from a text file into a class array with 250 storage space.
    *prompt the user for a word to search
    *display menu(1. find word via binary search, 2.find word via linear search, 3. Quit)
    *if selection is 1. do binary search, if word found output "found" and # of comparisons made.
    *if selection 2. do linear search ,if found display "found" and #of comparisons made
    *if not found, display, not found and number of comparisons made.

    -THE ISSUE IS THAT THE TEXT FILE IS NOT BEING READ INTO THE ARRAY PROPERLY SO THE SEARCHED DO RECORD #OF COMPARISONS OR FIND THE WORD SOUGHT.

    Code:
    void crunch::linearsearch(char wordFind[])         // wordFind passed in from main
      {
    for(int i=0;i <= Size;i++)
        {
       if(words[i] == wordFind)
          {
              cout <<"The word" <<" "<<wordFind<<" "<<"was found"<<" "
             <<i + 1<<" "<<"comparisons where made.";
              return;
          }
       else if(i == Size)
           {  
             cout <<"The word"<<wordFind<<"was not found"
            <<i + 1 <<"comparisons where made";
             return;
           }
         }
      }
       
    void crunch::binarysearch(char wordFind[])
      {  
        int first=0;
       int last=249;
    while (first <= last)
        { int count=0;
        int mid=(first + last)/ 2;
        
       if( words[mid] == wordFind)                     
         first= mid + 1;
        if(words[mid] > wordFind)
         first=mid - 1;
        if(words[mid]==wordFind)
               {
              cout <<"The word"<<wordFind<<"was found"
              <<count<<"comparisons where made.";
               }
         
       else if(count == Size)
           {
              cout <<" The word"<<wordFind<<"was not found"
              <<count<<"comparisons where made";
           }
              count++;
         }
      }
    int main()
    {
     char filename[20];
     int choice;
     char wordFind[23];                                                                                 
     crunch pez;
              
                
      cout <<" Enter the name of a file to read from:";
      cin >>filename;
      pez.put(filename);
    
    cout <<"1.Find words using linear search.\n";
    cout <<"2.Finds words using binary search.\n";
    cout <<"3.Quit";
    cin >>choice;
       
      if (choice==1)
        {
     
       cout <<"Enter word you wish to search.";
       cin >> wordFind;
        pez.linearsearch(wordFind);
        }     
      if(choice==2)
         {
       cout <<"Enter word you wish to search.";
       cin >> wordFind; 
     pez.binarysearch(wordFind);
         }
      else if(choice==3)
         return 0;  
    }
     
    Last edited by a moderator: Apr 25, 2012
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    Please don't create the same thread more than once. Other thread deleted.
     

Share This Page