0

I am solving speller pset5. In check50 my code is wrong. Don't know why. dictionary.c:// Implements a dictionary's functionality

#include <stdbool.h>
#include <strings.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#include "dictionary.h"

// Represents a node in a hash table


typedef struct node
{
    char word[LENGTH + 1];
    struct node *next;
}
node;
int sizell(node *head)
{
    node *temp = head;
    int size = 0;
    while (temp != NULL)
    {
        size++;
        temp = temp -> next;
    }
    return size;
}

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

// Hash table
node *table[N];

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

// Hashes word to a number
unsigned int hash(const char word[])
{
    unsigned int hash_value = 0;
        for (int i = 0, n = strlen(word); i < n; i++)
        {
             hash_value = (hash_value << 2) ^ word[i];
        }
        return hash_value % N; //N is size of hashtable
}

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    FILE *dict = fopen(dictionary, "r");
    if (dict == NULL)
    {
        return false;
    }
    char s[46];
    while (fscanf(dict, "%s", s) != EOF)
    {
        node *w = malloc(sizeof(node));
        if (w == NULL)
        {
            return false;
        }
        strcpy(w -> word, s);
        int il = hash(s);
        w -> next = table[il];
        table[il] = w;
    }
    return true;
}

// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
    int sizeh = 0;
    for (int i = 0; i < N; i++)
    {
        sizeh += sizell(table[i]);
    }
    return sizeh;
}

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

check50's output:

:) 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

Please Help :( .

  • at a quick glance it could be that the program does not // Unloads dictionary from memory, returning true if successful else false – DinoCoderSaurus Nov 13 '20 at 16:40
  • @DinoCoderSaurus So how to check if the whole hash table is freed or not? – Trisanu Das Nov 14 '20 at 8:09
  • unload only and always returns false,therefore speller only and always reports "could not unload" – DinoCoderSaurus Nov 14 '20 at 10:51
0

Regarding the check50 error messages, the problem is likely that your hash function is cash sensitive (in other words, "the" returns a different hash than "The").

Remember that the speller code will call your check function with the word exactly as it appears in the text.

You must log in to answer this question.

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