0

There are two problems with the code:

WORDS MISSPELLED are around five time normal. It is showing 5440 misspelled words while ideally there are around 900 such words(staff's solution).

In the bool functions, especially in the "load" and "unload" functions, I can't imagine how to return true and false. As for example, the function should return true when the fscanf reaches EOF, but it is the same case for returning false too. Kindly, analyse it.

Here is the Code, I have written:

    // Implements a dictionary's functionality

#include <stdbool.h>

#include "dictionary.h"

#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <strings.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 += 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;
}

The output:

WORDS MISSPELLED:     5440
WORDS IN DICTIONARY:  143091
WORDS IN TEXT:        17756
TIME IN load:         0.02
TIME IN check:        0.09
TIME IN size:         0.00
TIME IN unload:       0.01
TIME IN TOTAL:        0.13
2
  • Please edit the question and properly format the code. After you choose edit, select the code and click the {} in the format bar. – DinoCoderSaurus May 5 '20 at 15:33
  • Done. I hope it doesn't look horrible now – noob_dev17 May 5 '20 at 15:50
1

While this if (strcasecmp(point->word, word)==0) will do a case insensitive compare, this int m= hash(word); will return different values for "CAT" and "CaT" or "cat". Make sure the hash function is also case insensitive.

Regarding the bools: speller wants to know if load and unload ran successfully. In load there are two places where it could fail, the program executes return 1; in both cases. This is a problem since 1 is the boolean equivalent of true. In unload there is no condition where it doesn't run (save a segmentation fault, for example, in which case it doesn't return to speller) so it doesn't need to return a false. The return values (true or false) have no bearing on whether the functions produce the correct results or not; they just indicate to speller whether they've completed (or not).

3
  • Thanks, I changed the hash function, now the program is working fine. But still when I increase the number of 'N', it prompts "Seg fault". I wanna know why, if value of N is 1000 or less than that, then it's fine, but if increase it, prompts. Here is my code: // 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; } – noob_dev17 May 6 '20 at 8:28
  • I am still waiting for the response. – noob_dev17 May 8 '20 at 14:44
  • Where is the seg fault coming from? This post is a good guide to using the debugger. Suggest using small dictionary for sanity sake. You might also find valgrind -v helpful, since the seg fault indicates some memory violation, and valigrind will "show" them. If those tools don't result in success, it should at least narrow down the problem, which can then be posted as a new question. – DinoCoderSaurus May 8 '20 at 16:07

You must log in to answer this question.

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