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 MAX_STRING_SIZE 256
#define HASH_TABLE_SIZE 100003
struct Message {
long mtype;
int key;
};
typedef struct WordCountNode {
char *word;
int count;
struct WordCountNode *next;
} WordCountNode;
unsigned int hash_function(const char *str){
unsigned int hash =5381;
for (const char *s = str; *s !='\0'; s++){
hash =((hash <<5)+ hash)+(*s);
}
return hash % HASH_TABLE_SIZE;
}
void insert_word(WordCountNode **hash_table, const char *word){
unsigned int index = hash_function(word);
WordCountNode *current_node = hash_table[index];
while (current_node != NULL){
if (strcmp(current_node->word, word)==0){
current_node->count++;
return;
}
current_node = current_node->next;
}
WordCountNode *new_node = malloc(sizeof(WordCountNode));
if (new_node == NULL){
perror("Failed to allocate memory for new word");
exit(EXIT_FAILURE);
}
new_node->word = malloc((strlen(word)+1)* sizeof(char));
if (new_node->word == NULL){
free(new_node);
perror("Failed to allocate memory for word string");
exit(EXIT_FAILURE);
}
strcpy(new_node->word, word);
new_node->count =1;
new_node->next = hash_table[index];
hash_table[index]= new_node;
}
int get_word_count(WordCountNode **hash_table, const char *word){
unsigned int index = hash_function(word);
WordCountNode *current_node = hash_table[index];
while (current_node != NULL){
if (strcmp(current_node->word, word)==0){
return current_node->count;
}
current_node = current_node->next;
}
return 0;
}
void free_hash_table(WordCountNode **hash_table){
for (int i =0; i < HASH_TABLE_SIZE; i++){
WordCountNode *current_node = hash_table[i];
while (current_node != NULL){
WordCountNode *temp = current_node;
current_node = current_node->next;
free(temp->word);
free(temp);
}
}
free(hash_table);
}
int main(int argc, char *argv[]){
if (argc !=2){
fprintf(stderr, "Usage: %s
", argv[0]);
exit(EXIT_FAILURE);
}
char *test_case_number = argv[1];
char input_filename[256];
char words_filename[256];
snprintf(input_filename, sizeof(input_filename), "input%s.txt", test_case_number);
snprintf(words_filename, sizeof(words_filename), "words%s.txt", test_case_number);
FILE *input_file = fopen(input_filename, "r");
if (input_file == NULL){
perror("Error opening input file");
exit(EXIT_FAILURE);
}
int matrix_size, string_size;
key_t shm_key, msg_key;
if (fscanf(input_file, "%d %d %d %d", &matrix_size, &string_size, &shm_key, &msg_key)!=4){
fprintf(stderr, "Error reading input file
");
fclose(input_file);
exit(EXIT_FAILURE);
}
fclose(input_file);
if (string_size > MAX_STRING_SIZE || string_size <=0){
fprintf(stderr, "Invalid string size
");
exit(EXIT_FAILURE);
}
size_t shm_size = sizeof(char[matrix_size][matrix_size][string_size]);
int shm_id = shmget(shm_key, shm_size, 0666);
if (shm_id ==-1){
perror("shmget failed");
exit(EXIT_FAILURE);
}
char (*shared_matrix)[matrix_size][string_size]= shmat(shm_id, NULL, 0);
if (shared_matrix ==(void *)-1){
perror("shmat failed");
exit(EXIT_FAILURE);
}
int msg_id = msgget(msg_key, 0666);
if (msg_id ==-1){
perror("msgget failed");
shmdt(shared_matrix);
exit(EXIT_FAILURE);
}
FILE *words_file = fopen(words_filename, "r");
if (words_file == NULL){
perror("words file open failed");
shmdt(shared_matrix);
exit(EXIT_FAILURE);
}
WordCountNode **hash_table = calloc(HASH_TABLE_SIZE, sizeof(WordCountNode *));
if (hash_table == NULL){
perror("hash table allocation failed");
fclose(words_file);
shmdt(shared_matrix);
exit(EXIT_FAILURE);
}
char word[MAX_STRING_SIZE];
while (fscanf(words_file, "%255s", word)!= EOF){
insert_word(hash_table, word);
}
fclose(words_file);
int total_diagonals =2* matrix_size -1;
int caesar_key =0;
size_t msg_size = sizeof(struct Message)- sizeof(long);
for (int diag =0; diag < total_diagonals; diag++){
int diag_sum_counts =0;
for (int i =0; i < matrix_size; i++){
int j = diag - i;
if (j >=0 && j < matrix_size){
char *encoded_word = shared_matrix[i][j];
char decoded_word[MAX_STRING_SIZE];
int k;
int shift = caesar_key %26;
}
}
shmdt(shared_matrix);
free_hash_table(hash_table);
return 0;
}
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 blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!