Pleasee help with C! And follow instructions and please please do not copy other
ID: 3816212 • Letter: P
Question
Pleasee help with C! And follow instructions and please please do not copy other answers!Many thanks 1. (100 pts) Write a program to find the frequency of words in a file. You need to use dynamic memory allocation for this assignment. Use array of pointers to store the words and frequencies. You can use calloc0 to allocate the array. Set array size to 1000 and initialize all the pointers to NULL. Structure declaration to store words and frequencies is as follows struct word freq int count; char *word; When you see a word for the first time, insert into the array with count 1. If the word read from file is already in the array, increase its count. In this structure, you need to dynamically allocate the space for each word using malloc0. Use argc and argue for input file and output file. Sample execution of the program is given below. words tart is the input file which contains one word per line. freguen tut is the file to be generated cies by your program. It contains frequencies and words, one word and its frequency per line. elk05> assign6 words .txt frequencies.txt Sample input file is given below apple apple orange Output file for above input is given below 2 apple 3 orange 1 banana Don't forget to deallocate all the space allocated using malloc0 and calloc0 using free0 function. Run your program under valgnind as shown below to verify that you have no memory leaks. elk 05> valgrind assign6 vorda.txt frequencies.txt Somewhere in the output it should say All heap blocks are freed.
Explanation / Answer
main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 1000
typedef struct {
int count;
char *word;
} wordfreq;
void gen_freq(FILE *infile, FILE *outfie);
void add_word(wordfreq *words[], char *word, int *pos);
int main(int argc, char *argv[])
{
if(argc != 3)
{
fprintf(stderr, "Usage: %s [infile] [outfile] ", argv[0]);
exit(1);
}
FILE *infile = fopen(argv[1], "r");
if(infile == NULL)
{
fprintf(stderr, "Error opening file %s ", argv[1]);
exit(1);
}
FILE *outfile = fopen(argv[2], "wb");
if(outfile == NULL)
{
fprintf(stderr, "Error opening file %s ", argv[2]);
exit(1);
}
gen_freq(infile, outfile);
fclose(infile);
fclose(outfile);
return 0;
}
void gen_freq(FILE *infile, FILE *outfile)
{
/* Index of words array */
int count = 0;
int i;
char buf[100];
char c;
/* Initialize array of MAX_SIZE wordfreq pointers to NULL */
wordfreq *words[MAX_SIZE];
for(i = 0; i < MAX_SIZE; i++)
words[i] = NULL;
i = 0;
while((c = fgetc(infile)) != EOF)
{
if(c != ' ')
buf[i++] = c;
/* End of word so null-terminate then add to words array */
else
{
buf[i] = '';
i = 0;
add_word(words, buf, &count);
}
}
/* Output words and frequencies then free the word, then the struct */
for(i = 0; i < count; i++)
{
fprintf(outfile, "%d %s ", words[i]->count, words[i]->word);
free(words[i]->word);
free(words[i]);
}
}
void add_word(wordfreq *words[], char *word, int *pos)
{
int i;
int found = 0;
/* Check if word is in array */
for(i = 0; i < *pos && !found; i++)
if(strcmp(word, words[i]->word) == 0)
found = 1;
/* If word was found just increment its count */
if(found)
words[i - 1]->count++;
/* If word not found allocate struct and word itself setting count to 1 */
else
{
int len = strlen(word);
words[*pos] = (wordfreq *)malloc(sizeof(wordfreq));
words[*pos]->count = 1;
words[*pos]->word = (char *)calloc(len + 1, sizeof(char));
for(i = 0; i < len; i++)
words[*pos]->word[i] = word[i];
words[*pos]->word[len] = '';
/* Increment position of index in words array */
(*pos)++;
}
}
words.txt
apple
orange
apple
banana
orange
orange