0

I am completely stuck at pset5, and I don't know how to move on. I've tried to write code for all the four functions, but I strongly doubt that it is any good. It does compile, but 'speller.c' does not run, returning 'segmentation fault' without any additional information. I know that there must be a way to run through the code line by line or step by step, so as to determine what is happening exactly, but I don't know how to do it (being entirely new to programming). I've read many other threads about this pset, but I seem to be completely lost. Any advice would be much appreciated!

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

//define 'dicword' with length of max. length + 1 (for null)
char dicword[LENGTH+1];
//define node structure to make linked lists possible. A node consist of a dicword (char) and a pointer to another node.
typedef struct node
{
    char dicword[LENGTH+1];
    struct node *next;
}
node;

//create a hash table (size 2)
#define HASHMAX 2
node *hashtable[HASHMAX];


//define a hash function (using the simple one first just to try)
unsigned int hash(const char *str)
{
    int sum = 0;
    for (int j = 0; str[j]!='\0'; j++)
    {
        sum+= str[j];
    }
    return sum % HASHMAX;
}
//create counter for dictionary words
int dicwords = 0;

//CHECK Returns true if word is in dictionary else false.

bool check(const char *word)
{
    //hash the word to receive index for hashtable
    int index = hash(word);
    //input index in hashtable to retrieve the right llist. If the llist is empty, return false.
    if(hashtable[index]==NULL)
    {
        return false;
    }
    //if llist is not empty, run through list and compare lowword with each element of that list, untill end of list (null) is reached.
    else
    {
       node *cursor = hashtable[index];
       while(cursor != NULL)
       {
           if(strcasecmp(cursor -> dicword, word) ==0)
           {
               return true;
           }

           cursor = cursor->next;
       }
       return false;
    }
}



//LOAD Loads dictionary into memory. Returns true if successful else false.    
bool load(const char *dictionary)
{
    //set values in hash table to null
    for(int h = 0; h < HASHMAX; h++)
    {
        hashtable[h] = NULL;
    }
    //open dictionary for reading
    FILE *dic = fopen(dictionary, "r");
     if (dic == NULL)
     {
         return false;
     }
    char *newword = malloc(LENGTH+1);
    //scan dictionary (fp) and store each string until space in 'word'
    while (fscanf(dic,"%s\n", newword)!=EOF)
    {
        node *new_node = malloc(sizeof(node));
        //if malloc fails, return false
        if(new_node == NULL)
        {
           unload();
           return false;
        }
        //if malloc works, copy 'word' to the word-space in the new node
        else
        {
            strcpy(new_node->dicword, newword);
            dicwords++;

        }
        //allocate the new node to the correct list, using hash(word).
            new_node->next=hashtable[hash(newword)];
            hashtable[hash(newword)]->next=new_node;
     }
      return true;

}  

/**
 * Returns number of words in dictionary if loaded else 0 if not yet loaded.
 */
unsigned int size(void)
{
    if (dicwords>0)
    {
        return dicwords;
    }
    else
    {
      return 0;  
    }
}

/**
 * Unloads dictionary from memory. Returns true if successful else false.
 */
bool unload(void)
{
    for (int u = 0; u<HASHMAX; u++)
    {
        node *cursor = hashtable[u];
        while(cursor != NULL)
        {
            node *temp = cursor;
            cursor = cursor->next;
            free(temp);
        }
    }
    return false;

}
2
  • Have you used debug50? If not you MUST teach yourself how to use it. It allows you to step through the code line-by-line while it's running and see where it's breaking with the seg fault (which is a very common error in speller - you're probably breaking the links in the linked list). Knowing how to run a debugger is a critical skill.
    – 1fatcat
    Aug 13, 2017 at 15:54
  • Will do! I used debug50 in one of the previous sets, but only briefly and then kind of forgot about it.
    – AmLam
    Aug 13, 2017 at 18:32

1 Answer 1

0
        new_node->next=hashtable[hash(newword)];
        hashtable[hash(newword)]->next=new_node

Is wrong, I let you think why. The correct way would be the following

        new_node->next = hashtable[hash(newword)];
        hashtable[hash(newword)] = new_node
1
  • Thanks! That made the program run at least :) Not working properly yet, but that's ok- at least now the output that makes a bit more sense.
    – AmLam
    Aug 13, 2017 at 18:31

You must log in to answer this question.

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