the following function is my load - check functions. I believe one of two have the issue but not sure which one. Thanks for the help. My cs50 checks are like the following :
WORDS MISSPELLED: 5440
WORDS IN DICTIONARY: 143091
WORDS IN TEXT: 17756
TIME IN load: 0.02
TIME IN check: 0.06
TIME IN size: 0.00
TIME IN unload: 0.01
TIME IN TOTAL: 0.09
CLASS SOLUTION: WORDS MISSPELLED: 955
WORDS IN DICTIONARY: 143091
WORDS IN TEXT: 17756
TIME IN load: 0.03
TIME IN check: 0.01
TIME IN size: 0.00
TIME IN unload: 0.02
TIME IN TOTAL: 0.06
// Returns true if word is in dictionary else false
bool check(const char *word)
{
int index = hash(word);
if (table[index] == NULL)//nothing loaded here (no link to the head)
{
return false;
}
node *iter = table[index];//iterator pointer to jump on linked list = root
while (iter != NULL )// it could not find the word, keep searching
{
if (strcasecmp(iter -> word, word))
{
iter = iter ->next;
}
else if (!strcasecmp(iter -> word, word))
{
return true;
}
}
return false;
}
// Hashes word to a number
unsigned int hash(const char *word)
{
//between 0 N - 1 N = 5000
int count = strlen(word);
int sum = 0;
for ( int i = 0; i < count; i++)
{
sum += (int)word[i];
}
return round(sum % 5000);
}
// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
char wordx[LENGTH+1];
FILE *f = fopen(dictionary, "r");//opens the dictionary for reading
for (int i = 0; i < N ; i++)//appending all the nuls to the table
{
table[i] = NULL;
}
while (f == NULL)
{
printf("File could not opened.");//Checking file
return false;
}
while (fscanf(f, "%s", wordx) != EOF )//Until coming to the end of the file
{
node* p = (node *)malloc(sizeof(node));//creating a new node for each word
if (p == NULL)
{
printf("Memory could not allocated.");
return false;
}
strcpy(p -> word, wordx);//putting buffers word in to the node
int numindex = hash(wordx);
memset(wordx,0,LENGTH);//cleaning the buffer
node* iter = table[numindex]; //iterator pointer = root of the hash table
if (table[numindex] == NULL)//first appending to the hash table
{
table[numindex] = p; //appending "hashed" index function to the START of linked list
p -> next = NULL; // appended function now points null
n++;
}
else
{
table[numindex] = p;//head pointing new node
p-> next = iter;//new node pointing to the old node
n++;
}
}
fclose(f);
return true;
}
// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
return n ;
}