Need help with C hash table program at lines ENTER CODE HERE #include #include #include

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Need help with C hash table program at lines ENTER CODE HERE #include #include #include

Post by answerhappygod »

Need help with C hash table program at lines ENTER CODEHERE
#include <stdio.h>#include <stdlib.h>#include <stdbool.h>#include <string.h>#include <time.h>#include "hw8_fsm.h"
#define MAXSTRING 200#define HASHSIZE 400
// finite state machine states (defined in hw8_fsm.h)/* #define STARTSTATE 0 *//* #define S1 1 *//* #define S2 2 *//* #define S3 3 *//* #define S4 4 *//* #define S5 5 *//* #define S6 6 *//* #define ACCEPTSTATE 10 *//* #define ERRORSTATE 11 */
// ******** key value pairs *******typedef struct kv { char key [MAXSTRING]; int value; struct kv* next;} keyvalue_t;
// create new keyvalue pair with key s and value ikeyvalue_t* newKeyValue(char* s, int i) { keyvalue_t* t =(keyvalue_t*)malloc(sizeof(keyvalue_t)); if (t != NULL) { strcpy(t->key, s); t->value = i; t->next = NULL; }
return t;}
// free a used keyvalue pairvoid freeKeyValue(keyvalue_t* kvp) { if (kvp != NULL) { free(kvp); }}
// ******** hash table manipulation *******// hash 1 is just the length of the stringint hash1(char* s) { return (strlen(s) % HASHSIZE);}
// hash 2 is the sum of the char codes of string -// hint: use (int)s to get the integer code of characterin sint hash2(char* s) { int sumOfS = 0;
//**** YOUR CODE GOES HERE ****
return (sumOfS % HASHSIZE);}
// hash 3 is the prioduct of the first two char codes ofstringint hash3(char* s) { long productOfS = 1;
//**** YOUR CODE GOES HERE ****
return ((int)(productOfS % HASHSIZE));}
// add (key k, value v) to hashtable t at location loc// hint: use newKeyValue(k,v) to create a new kv pair// note that a t is an array of keyvalue pointers// return true if successful, false if notbool addToHashTable(keyvalue_t* t[], int loc, char* k, int v){ bool result = true;
//**** YOUR CODE GOES HERE ****
return result;}
// print the linked list pointed to by pvoid printHashList(keyvalue_t* p){ while (p != NULL) { printf("key/value: [%s] /[%d]\n",p->key,p->value); p = p->next; }
return;}
// print the hash table tvoid printHashTable(keyvalue_t* t[]) { int i; printf("====================\n"); for (i=0; i<HASHSIZE; i++) { printf("TABLE[%d]:\n",i); printHashList(t); printf("\n"); } printf("====================\n"); return;}
// free the hash list in table ht location ivoid freeHashList(keyvalue_t* ht[], int i){ keyvalue_t* t; while (ht != NULL){ t = ht; ht = ht->next; freeKeyValue(t); }
return;}
// free up kv pairs in hash table tvoid freeHashTable(keyvalue_t* t[]) { int i; for (i=0; i<HASHSIZE; i++) { freeHashList(t,i); }
return;}
// return true if t is emptybool isHashTableEmpty(keyvalue_t* t[]) { bool result = true; int i;
for (i=0; i<HASHSIZE; i++) { result = result && (t == NULL); }
return result;}
int main () {
char inputLine[MAXSTRING]; // temporary string tohold input line char cityStr[MAXSTRING]; // city name int lineNum; // line number (city rank) int popInt; // population int state; // FSM state int nextChar; // index of next character in input string char temp[MAXSTRING]; // tempstring to build up extracted strings from input characters int i; // loop variable // declare hash table as an array of pointers to keyvaluepairs keyvalue_t* hashtable1[HASHSIZE]; keyvalue_t* hashtable2[HASHSIZE]; keyvalue_t* hashtable3[HASHSIZE];
// initialize the hash table entries to NULL for (i=0; i<HASHSIZE; i++) { hashtable1 = NULL; hashtable2 = NULL; hashtable3[i] = NULL; }
FILE* fp; fp = fopen("pop.csv","r");
if (fp != NULL) { fgets(inputLine, MAXSTRING, fp); // prime the pumpfor the first line
while (feof(fp) == 0){
nextChar = 0; state = STARTSTATE; strcpy(temp,"");
if (nextChar >=strlen(inputLine)){ // if no input string then go toERRORSTATE state = ERRORSTATE; }
// read input, a line at a time and use FSMto parse the input while ((state != ERRORSTATE) && (state!= ACCEPTSTATE)) { state = stateMachine(state, nextChar, temp,inputLine, &lineNum, &popInt, cityStr);
// advance input nextChar++; } // end while state machineloop
// ***** END FINITE STATE MACHINE*****
// process the line - put it in each hashtable addToHashTable(hashtable1, hash1(cityStr),cityStr, popInt); addToHashTable(hashtable2, hash2(cityStr),cityStr, popInt); addToHashTable(hashtable3, hash3(cityStr),cityStr, popInt);
// get next line fgets(inputLine, MAXSTRING, fp); } // end while file input loop
fclose(fp); } else { printf("File not found!\n"); }
printf("\n\n***** HASH TABLE 1 *****\n\n"); printHashTable(hashtable1); printf("\n\n***** HASH TABLE 2 *****\n\n"); printHashTable(hashtable2); printf("\n\n***** HASH TABLE 3 *****\n\n"); printHashTable(hashtable3);
freeHashTable(hashtable1); freeHashTable(hashtable2); freeHashTable(hashtable3);
if (!isHashTableEmpty(hashtable1)) { printf("Hash Table One is NOT empty\n"); } if (!isHashTableEmpty(hashtable2)) { printf("Hash Table Two is NOT empty\n"); } if (!isHashTableEmpty(hashtable3)) { printf("Hash Table Three is NOT empty\n"); }
return 0;}

Need Help With C Hash Table Program At Lines Enter Code Here Include Stdio H Include Stdlib H Include Stdbool H 1
Need Help With C Hash Table Program At Lines Enter Code Here Include Stdio H Include Stdlib H Include Stdbool H 1 (38.02 KiB) Viewed 51 times
***** HASH TABLE 1 ***** ==== TABLE[0]: TABLE[1]: TABLE [2]: TABLE[3]: TABLE[4]: TABLE[5]: TABLE[6]: TABLE[7]: TABLE[8]: TABLE[9]: TABLE [10]: TABLE [11]: TABLE[12]: TABLE[13]: TABLE [14]: TABLE [15]: key/value: [Ames city, Iowa] / [58965] key/value: [Lehi city, Utah] / [47407] key/value: [Orem city, Utah] / [88328] TABLE [16]: key/value: [Logan city, Utah] / [48174] key/value: [Parma city, Ohio] / [81601] key/value: [Ogden city, Utah] / [82825] key/value: [Sandy city, Utah] / [87461] [112488] key/value: [Provo city, Utah] [124805] key/value: [Waco city, Texas] / key/value: [Akron city, Ohio] / [199110] TABLE[17]:
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply