Binary search algorithm

I am making a spellcheck program that compares words from a notepad file to another notepad dictionary file. I just need a bit of help in the binary search algorithm. How exactly do I report that there is a spelling mistake? How do I return a 0 or 1 from my function. I'm just slightly confused here.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <fstream>
#include <cstring>


using namespace std;

int spellcheck(char dict[][20], char wordfromletter[20], int sizeoffile) // Binary search function that takes in the current word, the dictionary array, and the size of the dictionary. 
{
    int first = 0;
    int last = sizeoffile - 1;
    int middle = (first + last) / 2;
    bool correct = false;


    while ((strcmp(dict[middle], wordfromletter)) != 0 && first <= last)
    {
        if (strcmp(dict[middle], wordfromletter) > 0)
        {
            first = middle + 1;
        }
        
        else 
        {
            last = middle - 1;
        }

        middle = (first + last) / 2;
    }

    if ()           // This is where I just need help. I don't know how to know whether there is a spelling mistake or not. 
        return 1;
    else 
    return 0;
}



int main() {

    ifstream input1;
    ifstream input2;
    char dict[200][20];
    char wordfromletter[20];
    int correction = 0;
    int sizeoffile = 0;

    input1.open("dictionary.txt");
    input2.open("letter.txt");

    if (input1.fail()) {
        cout << "error opening file" << endl;
        system("Pause");
        return 0;
    }

    if (input2.fail()) {
        cout << "error opening letter" << endl;
        system("Pause");
        return 0;
    }


    for (int n = 0; !input1.eof(); n++)
    {
        input1 >> dict[n];
        ++sizeoffile;
    }


/*  
    Printing out the dictionary
    for (int p = 0; p < 20; p++)
    {
        cout << dict[p] << endl;
    }
    */

    while (!input2.eof())
    {
        input2 >> wordfromletter;
        correction = spellcheck(dict, wordfromletter, sizeoffile);
        if (correction == 0)
        {
            cout << wordfromletter << "    spelling mistake" << endl;
        }
    }

    input1.close();
    input2.close();

    system("pause");
}
Why are you returning integers? It makes more sense to return bool. 1 for found and 0 for not found, right?

Anyways, here's a little snippet of code from a similar binary search. In this case, the person was searching through a Set class and returning an iterator. But in principle they are attempting to do the same thing as you are.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
template <class T>
SetIterator<T> Set<T> :: find(const T & t) const
{
   int iBegin = 0;
   int iEnd   = numItems - 1;
   
   while (iBegin <= iEnd)
   {
      int iMiddle = (iBegin + iEnd) / 2;

      // If we hit our target..
      if (t == data[iMiddle])
      {
         // beam me up scotty
         return SetIterator<T>(data + iMiddle);
      }
      else if (t < data[iMiddle]) // Aim lower
      {
         iEnd = iMiddle - 1;
      }
      else // Aim higher
      {
         iBegin = iMiddle + 1;
      }      
   }

   // If all else fails, return end()
   return SetIterator<T>(data + numItems);
}


Idk if sharing it is really that helpful. But basically check if first and last cross.
Last edited on
Topic archived. No new replies allowed.