The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Apr 2017 |
| Last Sign in: | 103 Weeks Ago, 3 Days Ago |
| Questions Answered: | 4870 |
| Tutorials Posted: | 4863 |
MBA IT, Mater in Science and Technology
Devry
Jul-1996 - Jul-2000
Professor
Devry University
Mar-2010 - Oct-2016
This is C programming assingment. I am getting a segmentation error running this code in the terminal on linux when I run this.
I have attached the assignment and my code. I need the code running per the assignment by 11/11/2015 at 7pm EST.
I'm not particularly proficient in C, so this code is probably going to look like a mess. Ask any questions you need, please.
150:198:211 C and Systems ProgrammingAssignment 4Assignment 4Due Wednesday, Nov. 11, 2015As in Assignment 3, you are to write a C program that extracts all words from a piece of text.This time, however, you are to store the words in ahash tableas discussed in class (see LectureNotes 8). The hash table should use chaining to resolve collisions. That is, words that hash tothe same “bucket” should be stored as a singly linked list of nodes for that bucket.Awordis a contiguous sequence of characters delimited by any of the following punctuationcharacters: the space character, period (.), comma (,), semicolon (;), colon (:), exclamationpoint (!), double quote ("), question mark (?), and newline character (\n). Note that the singlequote or apostrophe (') is not considered a delimiter. As a result, the following are consideredsingle words:sheep's 'Tis you're Charles'Two words are considered the same if they consist of exactly the same sequence of characters,ignoring case. For example, “Boy” and “boy” are considered the same word. Words should bestored inlower-casein the hash table.Each word in the hash table should correspond to a unique node in the linked list for the bucketit hashes to. Each node should be a structure consisting of the following members:•word:a string representing the word•count:the number of times thatwordappears in the text (ignoring case). That is, thefirst timewordis encountered in the text, insert it into the hash table by creating a newnode for it and initialize itscountto 1. For each subsequent occurrence ofwordin thetext, simply increment its node’scountby 1.•next:a pointer to the next node in the list (if it exists)A sample type definition for a node is shown below (which is used in the subsequence):struct NodeType {char *word;int count;struct NodeType *next;};typedef struct NodeType Node;The size (i.e., number of buckets) of the hash table, sayhtsize, should be specified as acommand-line argument. Makehtsizea global variable. Upon readinghtsize, allocatefromthe heapa hash table withhtsizebuckets, each initially empty.Your program should read several lines of text stored in a file and redirected to the standardinputstdin. It should then parse the lines into individual words and store each word –inlower-case– into the hash table.All nodes created in the hash table should be allocated fromthe heap.After reading the entire text, print all the[word,count]tuples stored in the hashtable (see below). Finally, destroy the hash table by freeing all the heap space allocated to it.
/*File concordance.cSolution to CS 211 Assignment 3*/#include<stdio.h>#include<stdlib.h>#include<string.h>#include <assert.h>#define HASH_MULTIPLIER 65599#define ARRAYSIZE 1031//Define struct NodeTypestruct NodeType{char *word;int count;struct NodeType* next;};typedef struct NodeType Node;//global variable for hash table sizeint htsize;//Prototypeschar* getToken(FILE *file);unsigned int hash(const char *str);char* lowercase(char *str);Node **ht_create();int ht_insert(Node **Table, const char *word);void ht_print(Node **Table);void ht_destroy(Node **Table);int main(int argc, char **argv[]){const char *fileName;//convert to inthtsize = atoi(argv[1]);fileName = argv[3];FILE *file = fopen(fileName, "r");//assert(file != 0);Node** Table = ht_create();char *word;int count = 0;while (!feof(file) ){word = getToken(file);int i = 0;//convert to lowercase, fill hashtableif (word != NULL){for ( i = 0; word[i]; i++){word[i] = lowercase(word[i]);}ht_insert(Table, word);}count++;}//Print Tableht_print(Table);//Destroy Tableht_destroy(Table);
Attachments: