this is my code to count the frequency of words in a text file#.pdf
1. this is my code to count the frequency of words in a text file
#define _GNU_SOURCE
#include
#include
#include
#include
/******************************
here is the Structure for storing words
received from each thread
******************************/
struct return_vall{
char wordlist[100][100];
int count[100];
} *arr;
pthread_t *threads;
int lines;
/*****************************
here are Aggregates Frequency Sum of
different threads
*****************************/
int aggregate(char *a)
{
int sum = 0;
int i=0;
while(1)
{
if(i==lines) break;
int count = 0;
while(1)
{
if(arr[i].count[count] == -1)
{
break;
}
2. if(strcmp(a,arr[i].wordlist[count]) == 0)
{
sum += arr[i].count[count];
arr[i].count[count] = 0;
}
count++;
}
i++;
}
return sum;
}
/**************************
Function thta are executed by each
thread on seperate line.
**************************/
void *word_count(void* num)
{
int *ln = num;
unsigned int line_number = *ln;
line_number++;
//printf("Thread %d ",line_number);
char cmd_p1[9] = "sed -n '0";
char cmd_p2[2];
sprintf(cmd_p2,"%d",line_number);
char cmd_p3[14] = "p' 'file.txt'0";
char command[100];
6. }
Solution
this is my code to count the frequency of words in a text file
#define _GNU_SOURCE
#include
#include
#include
#include
/******************************
here is the Structure for storing words
received from each thread
******************************/
struct return_vall{
char wordlist[100][100];
int count[100];
} *arr;
pthread_t *threads;
int lines;
/*****************************
here are Aggregates Frequency Sum of
different threads
*****************************/
int aggregate(char *a)
{
int sum = 0;
int i=0;
while(1)
{
if(i==lines) break;
int count = 0;
while(1)
{
if(arr[i].count[count] == -1)
7. {
break;
}
if(strcmp(a,arr[i].wordlist[count]) == 0)
{
sum += arr[i].count[count];
arr[i].count[count] = 0;
}
count++;
}
i++;
}
return sum;
}
/**************************
Function thta are executed by each
thread on seperate line.
**************************/
void *word_count(void* num)
{
int *ln = num;
unsigned int line_number = *ln;
line_number++;
//printf("Thread %d ",line_number);
char cmd_p1[9] = "sed -n '0";
char cmd_p2[2];