I'm was finished with cs50 pset5 speller(or so I thought), when I ran my complete program for the first time. It started printing out some words, so I was like: "Bingo! I'm done!" and I was seriously happy because I've been working on it for weeks. But then, while it was going through the words, I noticed some words like the word 'and' printed out when they were in the dictionary. So I thought that I had a small bug in my code. But when it came to the end of the list, It didn't do the 'Words misspelled:' Part and the other parts. It didn't even make a new line starting with $~
. I had to close up that entire terminal because I couldn't do anything else there since it was like this:
OUT
THE
END
Those words were the last words, and then there's just an empty line. Then I opened a new terminal and checked my answers with the staff's answers, and SO much was wrong! I tried to find some bugs in my code, but I couldn't find any. If you can find some and tell me, I REALLY appreciate it. Here's my code:
// Implements a dictionary's functionality
#include <ctype.h>
#include <stdio.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <string.h>
#include <stdlib.h>
#include "dictionary.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 = 19683;
// Hash table
node *table[N];
// Returns true if word is in dictionary else false
bool check(const char *word)
{
unsigned int lol = hash(word);
// TODO
int i;
node *cursor =table[lol];;
while(cursor != NULL)
{
if(strcasecmp(word, cursor -> word) == 0)
{
return true;
}
cursor = cursor->next;
}
return false;
}
// Hashes word to a number
unsigned int hash(const char *word)
{
int count = 0;
// TODO
for(int i = 96; i < 122; i++)
{
for(int j = 96; j < 122; j++)
{
for(int d = 96; d < 122; d++)
{
count++;
char firstletter = i;
char secondletter = j;
char thirdletter = d;
if(word[0] == firstletter&& word[1] == secondletter && word[2] == thirdletter)
{
return count;
}
}
}
}
return 1;
}
// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
// Open dictionary and check for memory issue
// Open dictionary file and create word array
FILE *dict = fopen(dictionary, "r");
char word[LENGTH + 1];
// Check for memory issue with dict
if(dict == NULL)
{
printf("Dictionary is null\n");
unload();
return false;
}
// Read string 1 word at a time
while (fscanf(dict, "%s", word) != EOF)
{
node *n = malloc(sizeof(node));
if (n == NULL)
{
return false;
}
strcpy(n -> word, word);
// Index word using hash function
int dict_index = hash(word);
// Insert into hash table if already empty
if (table[dict_index] == NULL)
{
n -> next = NULL;
}
// Insert work as new node if not empyty
else
{
n -> next = table[dict_index];
}
table[dict_index] = n;
}
// Close dictionary file
fclose(dict);
// Indicate success
return true;
}
// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
int count = 0;
// TODO
for(int i = 0; i < N; i++)
{
while(table[i] != NULL)
{
node* cursor = table[i];
count++;
cursor = cursor -> next;
}
}
return count;
}
// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
// TODO
for(int i = 0; i < N; i++)
{
node *cursor = table[i];
while(cursor)
{
node *temp = cursor;
cursor = cursor -> next;
if(temp != NULL)
{
return true;
}
free(temp);
}
}
return 1;
}
Thanks a lot, Lost in code.