Somewhere in the output it should say All heap blocks are freed. *Programs in C*
ID: 3815084 • Letter: S
Question
Somewhere in the output it should say All heap blocks are freed.
*Programs in C*
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 calloc() 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 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. elko5> assigne6 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. elko5> valgrind assign6 words.txt frequencies.txtExplanation / Answer
#include <stdio.h>
#include <string.h>
#include <ctype.h>
struct wordfreq{
int count;
char *word;
};
typedef struct wordfreq occur;
occur *s;
s=(occur *)calloc(30,sizeof(occur));
int update(struct wordfreq [], const char [], int);
int main()
{
char string[100], unit[20], c;
int i = 0, freq = 0, j = 0, count = 0, num = 0;
FILE *fp;
fp = fopen( “words.txt”, "r" );
if ( fp == NULL )
{
printf("Cannot open %s for reading ", filename );
exit(1); /* terminate program */
}
n = fgetc( fp ) ;
while ( n != EOF )
{
{
for (i=0; i<n; i++)
{
count=0;
for (j=0; j<n; j++)
{
if (number1[j] == number1[i])
{
count++;
}
}
number2[i] = count;
}
printf (" ");
FILE *op;
op=fopen("frequencies.txt","w");
for (i=0; i<n; i++)
{
printf ("Number %d: occurance %d times(s) ", number1[i], number2[i]);
}
fclose( op );
fclose(fp);
return 0;
}
free(s);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.