0

I am having a strange issue with the pset5. When I debug the file, it works fine, but when I check it through Check50, it throws several frowns at me. Need suggestions. Here is my code:

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <strings.h>
#include "dictionary.h"

// Represents a node in a hash table
typedef struct node
{
    char word[LENGTH + 1];
    struct node *next;
}
node;

// Number of buckets in hash table
const unsigned int N = 1000;

// Hash table
node *table[N];

// Number of Words
int numWords=0;

// Returns true if word is in dictionary else false
bool check(const char *word)
{
    // TODO
    int m= hash(word);
    node *point=table[m];
    while(point->next!=NULL)
    {
        if (strcasecmp(point->word, word)==0)
        {
            return true;
        }
        else
        {
            point = point->next;
        }
    }
    if (point->next==NULL)
    {
        if (strcasecmp(point->word, word)==0)
        {
            return true;
        }
    }
    return false;
}

// Hashes word to a number
unsigned int hash(const char *word)
{
    // TODO

        int sumAlph = 0;
        for (int a = 0, n= strlen(word); a<n; a++)
        {
            sumAlph += tolower(word[a]);
        }
    return sumAlph % N;
}

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    // TODO
    char object[LENGTH+1];
    int m;
    FILE *dict= fopen(dictionary, "r");
    if (dict == NULL)
    {
        return 1;
    }
    while (fscanf(dict, "%s", object) == 1)
    {
        node *nlln = malloc(sizeof(node));
        if (nlln == NULL)
        {
            return 1;
        }
        strcpy(nlln->word, object);
        numWords++;
        m = hash(nlln->word);
        nlln->next = table[m];
        table[m] = nlln;
    }
    fclose(dict);
    return true;
}
// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
    // TODO
    return numWords;
}

// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
    // TODO
    node *cursor=NULL,*tmp=NULL;
    for (int a=0; a<N; a++)
    {
        cursor = table[a];
        while(cursor->next != NULL)
        {
            tmp = cursor;
            cursor = cursor->next;
            free(tmp);
        }
        tmp=cursor;
        free(tmp);
    }
    return true;
}```

Here is the Check50 output:

```Results for cs50/problems/2020/x/speller generated by check50 v3.0.10
:) dictionary.c, dictionary.h, and Makefile exist
:) speller compiles
:( handles most basic words properly
    expected "MISSPELLED WOR...", not "MISSPELLED WOR..."
:( handles min length (1-char) words
    expected "MISSPELLED WOR...", not "MISSPELLED WOR..."
:( handles max length (45-char) words
    expected "MISSPELLED WOR...", not "MISSPELLED WOR..."
:( handles words with apostrophes properly
    expected "MISSPELLED WOR...", not "MISSPELLED WOR..."
:( spell-checking is case-insensitive
    expected "MISSPELLED WOR...", not "MISSPELLED WOR..."
:( handles substrings properly
    expected "MISSPELLED WOR...", not "MISSPELLED WOR..."
:| program is free of memory errors
    can't check until a frown turns upside down```
0

In your functions 'check' and 'unload', when you start to check the tables,in your whlile loops, you start by checking 'pointer to table[x]-> next'. If a particular table only contains one element, or if the element is the last of the list, you'll skip it, and the word stored there, won't be processed.

2
  • If you look closely, in the check function, i have an if statement below while loop, which is just to solve the same issue. And in unload too, i free the tmp two times, the second time is especially to unload current element.
    – noob_dev17
    Jun 7 '20 at 2:49
  • If you try to free an empty table your program will stop. If you try to check 'table[x]->next' against NULL and table[X] is empty, you´'ll get a Segmentation fault. When running your program with the small dictionary, it gets seg-fault the first time it tries to check an empty table. If you want to want your program to work with empty tables, try to avoid checking against '->next'. Also in your load function you're not setting to NULL your '->next' pointer. You are assigning directly table[X] to it. That could cause troubles too.
    – Tritum
    Jun 7 '20 at 7:52

You must log in to answer this question.

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