Question: The code provided is a frequency counter coded in C, and is designed to compile in: gcc -std=c99 -Wall -pedantic -o filename filename.c With no
The code provided is a frequency counter coded in C, and is designed to compile in:
gcc -std=c99 -Wall -pedantic -o filename filename.c With no warnings or Errors.
.
.
THE PROBLEM CONTAINS TWO PARTS:
1. Use the provided code for a frequency counter coded in C, to rearrange the code into 3 distinct files:
freq.c - just main and no other functions func.c - Contains additional functions that are not main() func.h - Contains prototype code for functions in func.c . Must be included from both freq.c and func.c . Include #ifndef to prevent multiple inclusion from occuring.
.
.
.
2. Provide a Makefile which provides the following:
-A default rule which **ONLY** compiles program to an executable named freq
-A clean rule which deletes .o files and executable file
-A run rule which will compile and run the program
The provided code:
.
.
.
#include #include
//methdo which return the index if word exists in array else -1
int existsIndex(char arr[201][25], char word[25], int count[201], int size)
{
//start from 0
for (int i = 0; i < size; i++)
{
if (strcmp(arr[i], word) == 0)
return i;
}
return -1;
}
//main driver code of the program
int main()
{
//it is said that there will be nomore than 200 unique words
//so create a aray of 200 size ,
//the count array wills store the respective count
char arr[201][25];
int count[201] = {0};
//run an indefinite loop
//this will exit when user enters zzzzz
char user_inp[25];
int size = 0;
while (1)
{
//get user input
scanf("%s", user_inp);
//compare suer inp
if (strcmp(user_inp, "zzzzz") == 0)
{
//exit from loop
break;
}
size++;
//else check if strign exists ,
int ind = existsIndex(arr, user_inp, count, size);
if (ind != -1)
{
//increase the count
count[ind] += 1;
}
else
{
//add the string
strcpy(arr[size - 1], user_inp);
count[size - 1] = 1;
}
}
//now sort the count array and also the arr along with it
for (int i = 0; i < size - 1; i++)
{
for (int j = 0; j < size - i - 1; j++)
{
if (count[j] < count[j + 1])
{
int t = count[j];
count[j] = count[j + 1];
count[j + 1] = t;
char te[25];
strcpy(te, arr[j]);
strcpy(arr[j], arr[j + 1]);
strcpy(arr[j + 1], te);
}
}
}
//finally display the top 5
printf("Count Word ");
printf("====== ====== ");
for (int i = 0; i < (5 < size ? 5 : size); i++)
{
printf("%d %12s ", count[i], arr[i]);
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
