Write a program to find the frequency of words in a file. You need to use dynami
ID: 3816948 • Letter: W
Question
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. Set array size to 1000 and initialize all the pointers to NULL. Structure declaration to store words and frequencies is as follows struct wordfreq {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 malloc(). Use argc and argv for input file and output file. Sample execution of the program is given below. words.txt is the input file which contains one word per line. frequencies.txt is the file to be generated 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 orange apple banana orange 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 malloc() and calloc() using free() function. Run your program under valgrind as shown below to verify that you have no memory leaks. elk05 > valgrind assign6 words.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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.