0

The function is given a string (for example "communism") and checks whether or not its suffix is a part of a wordbank - the program takes an absurdly long time (or is infinite-looped, I don't know but I suspect the first). here's an example of the suffix wordbank (reversed) char* noun_suffixes[] = { "msi", "re", "sci", "ssen", "tnem", "tsi" }; Here's the function: (n is the number of words in the bank, *str is the word).

bool is_suffix_in_dict(char* str, char* dict[], int n)
{
    printf("first checkpoint reached");
    char strRev[MAX_LEN + 1] = { 0 }, strToCheck[MAX_LEN + 1] = { 0 };
    unsigned int len = strlen(str);
    for (unsigned int i = 0; i < len; i++)
    {
        strRev[len - i] = *(str + i);
    }
    for (unsigned int i = 0, j = 0; i < len; i++)
    {
        while (strRev[i] < 'a' || strRev[i] > 'z')
        {
            i++;
        }
        if (i < len)
        {
            strToCheck[j] = strRev[i];
            if (binarySearch(dict, n, strToCheck))
            {
                printf("second check reached - positive");
                return TRUE;
            }
            j++;
        }
    }
    printf("second check reached - neg");
    return FALSE;
}

the function calls upon this one-

bool binarySearch(char* dict[], int n, char s[MAX_LEN + 1])
{
    int last = n, first = 0, mid = n / 2;
    while (first < n)
    {
        int cmpVal = strcmp(dict[mid], s);
        if (cmpVal < 0)
        {
            printf("cmpVal<0");
            first = mid + 1;
            mid = (first + last) / 2;
        }
        else if (cmpVal > 0)
        {
            printf("cmpVal>0");
            last = mid;
            mid = (first + last) / 2;
        }
        else
        {
            return TRUE;
        }
    }
    return FALSE;
}
kal_elk122
  • 143
  • 6
  • 3
    Perfect time to get familiar with your debugger and single step through the program. – Lundin Jan 13 '21 at 15:00
  • 1
    `while (first < n)` will be an inifinite loop when `return TRUE;` is not executed and `last = mid;` is executed. (not thinking well, but some exception may exists?) – MikeCAT Jan 13 '21 at 15:02
  • Your binary search is probably broken — the condition `while (first < n)` should probably be `while (first < last)`. If the prefix is not in the list, your loop is going to run for a long time. Binary search is notoriously hard to get right. Test it carefully! – Jonathan Leffler Jan 13 '21 at 15:02
  • Edit - It is infinitely looped - I placed a printf after the while in the Binary_search function and it printed plenty of times - still can't figure out why though – kal_elk122 Jan 13 '21 at 15:03
  • I don't really have a good debugger in my program - you're right though I'll ask my teachers and stop bothering you guys – kal_elk122 Jan 13 '21 at 15:03
  • Note that it helps you get answers if you create an MCVE ([Minimal, Complete, Verifiable Example](https://stackoverflow.com/help/mcve) — or MRE or whatever name SO now uses) or an SSCCE ([Short, Self-Contained, Correct Example](http://sscce.org/)). That should include sample data and expected and actual results. – Jonathan Leffler Jan 13 '21 at 15:05
  • Thanks! that was it! – kal_elk122 Jan 13 '21 at 15:05
  • Will do my best to implement the notes given next time - thank you! – kal_elk122 Jan 13 '21 at 15:08
  • If it's any consolation, Jon Bentley reports in his book [Programming Pearls, 2nd Edn](https://smile.amazon.com/dp/0201657880/) that Knuth points out that the first binary search program was published in 1946 but the first correct one was not published until 1962. See also Google's [Extra, Extra — Read All About It: Nearly All Binary Searches and Merge Sorts are Broken](https://ai.googleblog.com/2006/06/extra-extra-read-all-about-it-nearly.html). – Jonathan Leffler Jan 13 '21 at 15:13
  • It is, thanks haha – kal_elk122 Jan 13 '21 at 16:54

0 Answers0