Question: #include #include #include #include #include #include #include #include #define MAX _ STRING _ SIZE 2 5 6 #define HASH _ TABLE _ SIZE 1 0
#include
#include
#include
#include
#include
#include
#include
#include
#define MAXSTRINGSIZE
#define HASHTABLESIZE
struct Message
long mtype;
int key;
;
typedef struct WordCountNode
char word;
int count;
struct WordCountNode next;
WordCountNode;
unsigned int hashfunctionconst char str
unsigned int hash ;
for const char s str; s ; s
hash hash hashs;
return hash HASHTABLESIZE;
void insertwordWordCountNode hashtable, const char word
unsigned int index hashfunctionword;
WordCountNode currentnode hashtableindex;
while currentnode NULL
if strcmpcurrentnodeword, word
currentnodecount;
return;
currentnode currentnodenext;
WordCountNode newnode mallocsizeofWordCountNode;
if newnode NULL
perrorFailed to allocate memory for new word";
exitEXITFAILURE;
newnodeword mallocstrlenword sizeofchar;
if newnodeword NULL
freenewnode;
perrorFailed to allocate memory for word string";
exitEXITFAILURE;
strcpynewnodeword, word;
newnodecount ;
newnodenext hashtableindex;
hashtableindex newnode;
int getwordcountWordCountNode hashtable, const char word
unsigned int index hashfunctionword;
WordCountNode currentnode hashtableindex;
while currentnode NULL
if strcmpcurrentnodeword, word
return currentnodecount;
currentnode currentnodenext;
return ;
void freehashtableWordCountNode hashtable
for int i ; i HASHTABLESIZE; i
WordCountNode currentnode hashtablei;
while currentnode NULL
WordCountNode temp currentnode;
currentnode currentnodenext;
freetempword;
freetemp;
freehashtable;
int mainint argc, char argv
if argc
fprintfstderr "Usage: s
argv;
exitEXITFAILURE;
char testcasenumber argv;
char inputfilename;
char wordsfilename;
snprintfinputfilename, sizeofinputfilename "inputstxt testcasenumber;
snprintfwordsfilename, sizeofwordsfilename "wordsstxt testcasenumber;
FILE inputfile fopeninputfilename, r;
if inputfile NULL
perrorError opening input file";
exitEXITFAILURE;
int matrixsize, stringsize;
keyt shmkey, msgkey;
if fscanfinputfile, d d d d &matrixsize, &stringsize, &shmkey, &msgkey
fprintfstderr "Error reading input file
;
fcloseinputfile;
exitEXITFAILURE;
fcloseinputfile;
if stringsize MAXSTRINGSIZE stringsize
fprintfstderr "Invalid string size
;
exitEXITFAILURE;
sizet shmsize sizeofcharmatrixsizematrixsizestringsize;
int shmid shmgetshmkey, shmsize, ;
if shmid
perrorshmget failed";
exitEXITFAILURE;
char sharedmatrixmatrixsizestringsize shmatshmid NULL, ;
if sharedmatrix void
perrorshmat failed";
exitEXITFAILURE;
int msgid msggetmsgkey, ;
if msgid
perrormsgget failed";
shmdtsharedmatrix;
exitEXITFAILURE;
FILE wordsfile fopenwordsfilename, r;
if wordsfile NULL
perrorwords file open failed";
shmdtsharedmatrix;
exitEXITFAILURE;
WordCountNode hashtable callocHASHTABLESIZE, sizeofWordCountNode ;
if hashtable NULL
perrorhash table allocation failed";
fclosewordsfile;
shmdtsharedmatrix;
exitEXITFAILURE;
char wordMAXSTRINGSIZE;
while fscanfwordsfile, s word EOF
insertwordhashtable, word;
fclosewordsfile;
int totaldiagonals matrixsize ;
int caesarkey ;
sizet msgsize sizeofstruct Message sizeoflong;
for int diag ; diag totaldiagonals; diag
int diagsumcounts ;
for int i ; i matrixsize; i
int j diag i;
if j && j matrixsize
char encodedword sharedmatrixij;
char decodedwordMAXSTRINGSIZE;
int k;
int shift caesarkey ;
shmdtsharedmatrix;
freehashtablehashtable;
return ;
rewrite and humanize this code withoiut losing any logic.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
