0

the following function is my load - check functions. I believe one of two have the issue but not sure which one. Thanks for the help. My cs50 checks are like the following :

WORDS MISSPELLED: 5440

WORDS IN DICTIONARY: 143091

WORDS IN TEXT: 17756

TIME IN load: 0.02

TIME IN check: 0.06

TIME IN size: 0.00

TIME IN unload: 0.01

TIME IN TOTAL: 0.09

CLASS SOLUTION: WORDS MISSPELLED: 955

WORDS IN DICTIONARY: 143091

WORDS IN TEXT: 17756

TIME IN load: 0.03

TIME IN check: 0.01

TIME IN size: 0.00

TIME IN unload: 0.02

TIME IN TOTAL: 0.06

// Returns true if word is in dictionary else false
bool check(const char *word)
{
    int index = hash(word);

    if (table[index] == NULL)//nothing loaded here (no link to the head)
    {
        return false;
    }

    node *iter = table[index];//iterator pointer to jump on linked list = root

    while (iter != NULL )// it could not find the word, keep searching
    {
        if (strcasecmp(iter -> word, word))
        {
            iter = iter ->next;
        }

        else if (!strcasecmp(iter -> word, word))
        {
            return true;
        }
    }
        return false;
}

// Hashes word to a number
unsigned int hash(const char *word)
{
    //between 0 N - 1 N = 5000
    int count = strlen(word);

    int sum = 0;

    for ( int i = 0; i < count; i++)
    {
        sum += (int)word[i];
    }

    return round(sum % 5000);
}

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{

    char wordx[LENGTH+1];

    FILE *f = fopen(dictionary, "r");//opens the dictionary for reading

    for (int i = 0; i < N ; i++)//appending all the nuls to the table
    {
        table[i] = NULL;
    }

    while (f == NULL)
    {
        printf("File could not opened.");//Checking file
        return false;
    }
    while (fscanf(f, "%s", wordx) != EOF )//Until coming to the end of the file
    {
        node* p = (node *)malloc(sizeof(node));//creating a new node for each word

        if (p == NULL)
        {
            printf("Memory could not allocated.");
            return false;
        }

        strcpy(p -> word, wordx);//putting buffers word in to the node

        int numindex = hash(wordx);

        memset(wordx,0,LENGTH);//cleaning the buffer

        node* iter = table[numindex]; //iterator pointer = root of the hash table

        if (table[numindex] == NULL)//first appending to the hash table
        {

            table[numindex] = p; //appending "hashed" index function to the START of linked list

            p -> next = NULL; // appended function now points null

            n++;
        }
        else
        {

        table[numindex] = p;//head pointing new node

        p-> next = iter;//new node pointing to the old node

        n++;
        }
    }
    fclose(f);
    return true;
}
// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
     return n ;
}
3
  • Can you share your hash and size function also.
    – Vsjain
    Commented Jun 4, 2020 at 12:50
  • @Vsjain I was working on the code this is the current version but it still gives the worn g output. My concern is the load function I fixed the check function. Waiting for any help. :)
    – IDK
    Commented Jun 4, 2020 at 14:01
  • Why is N 5000? I would also recommend to take one char at time until it is '\n' and then increment n. Then set the position where '\n' is stored to be '\0'. hope this helps.
    – Vsjain
    Commented Jun 4, 2020 at 15:29

1 Answer 1

0

It looks like all of the misspelled words have a capital letter in them. That means one of two things. Either the check code is not handling mixed case, or the hash function isn't handling mixed case. Remember, if the hash function doesn't handle mixed case, it will return different hashes for "cat" and "Cat".

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. -)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .