Answer To: ENEE150_HW05 : due Tuesday 3/5 before 11:59pm (12 points) Both problems of this homework require use...
Pratik answered on Mar 06 2021
BubSortMain/helperfns.h
#define BUFFER 100
FILE *openFile(char *fileName,char flag);
char *readWord(FILE *f);
int writeWord(char *c,FILE *f);
long double calculateBoringness(char *word);
BubSortMain/helperfns.c
#include
#include tdio.h>
#define len(x) sizeof(x)/sizeof(x[0])
#define BUFFER 128
long double letterFrequency[26]=
{
0.08167,0.01492,0.02782,0.04253,0.12702,0.0228,
0.02015,0.06094,0.06966,0.00153,0.00772,0.04025,
0.02406,0.06749,0.07507,0.01929,0.00095,0.05987,
0.06327,0.09056,0.02758,0.00978,0.02360,0.00150,
0.01974,0.00074
};
FILE *openFile(char *fileName,char flag)
{
FILE *p;
if(flag == 'r')
p = fopen(fileName,"r");
else
p = fopen(fileName,"w");
return p;
}
char *readWord(FILE *f)
{
char *str = (char *)malloc(BUFFER*sizeof(char));
int index = 0;
char temp;
temp=(char)fgetc(f);
while(temp!='\n'&&temp!=EOF)
{
str[index++]=temp;
temp=(char)fgetc(f);
}
if(temp!=EOF)
str[index]='\0';
else
str[index]=temp;
return str;
}
int writeWord(char *str,FILE *f)
{
int l = len(str);
fwrite(str,sizeof(str),1,f);
fputc('\n',f);
return 1;
}
long double calculateBoringness(char *str)
{
long double x = 1.0;
int l=len(str);
for (int i = 0; i < l; ++i)
{
if(str[i]>='a'&&str[i]<='z')
x= x*letterFrequency[(int)(str[i]-'a')];
else if(str[i]>='A'&&str[i]<='Z')
x= x*letterFrequency[(int)(str[i]-'A')];
else return 1.0;
}
return x;
}
BubSortMain/bubsortmain.c
#include
#include
#include "helperfns.h"
#define WC 265000
char *words[WC];
char *temp;
int wordCount=0;
FILE *in,*out;
clock_t start, end;
double cpu_time_used;
void bubblesort()
{
int i;
for(i = 0; i < wordCount; ++i)
{
if(i>=1000)
break;
for(int j = wordCount-1; j >i; j--)
{
if(calculateBoringness(words[j]) {
temp=words[j];
words[j]=words[j-1];
words[j-1]=temp;
}
}
if(0==writeWord(words[i],out))
break;
}
}
int main()
{
in = openFile("word2.txt",'r');
out = openFile("Problem1_1000.txt",'w');
temp=readWord(in);
while(*temp!=EOF)
{
words[wordCount++]=temp;
temp=readWord(in);
}
start = clock();
bubblesort();
end = clock();
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
printf("cpu_time_used= %f seconds \n",cpu_time_used);
return 0;
}
BubSortMain/Makefile
bubsortmain : bubsortmain.c helperfns.c
gcc -o bubsortmain bubsortmain.c...