ENEE150_HW05 : due Tuesday 3/5 before 11:59pm (12 points) Both problems of this homework require use of malloc() and related functonss Please refer to ENEE150_HW00 to refresh your memory on rules for...

1 answer below »

ENEE150_HW05 : due Tuesday 3/5 before 11:59pm (12 points)
Both problems of this homework require use of malloc() and related functonss


Please refer to ENEE150_HW00 to refresh your memory on rules for homework.


Problem1/ bubsortmain.c, helperfns.c, helperfns.h, Makefle


Read the words from the included 'words2stxt' into a malloc’d bufer then use a simple bubblesort in-place (esgs directly on the strings in the bufer) to fnd the least boring words in the entre dictonaryr where the ‘boringness’ of a word is the product of the percentages of each leter in a word’s relatie to other leters in modern English (see htps://enswikipediasorg/wiki/deter_ffrequency)s For examples The boringness of the word ‘a’ is s08167 (iery boring)r while that of ‘the’ is 0s000700988627328 (less boring)s Haie your program stop when it has found the 1000 least boring words in the words2 list and haie it saie the frst thousand words of the sorted result to the fle ‘Problem1_f1000stxt’s Haie your program print out the CPU tme used when it fnishes (see how in the atached example‘fbb6benchsc’)s


Problem2/ indirectbubsortmain, helperfns.c, helperfns.h, Makefle


Defne a struct ‘wordentry’ which holds a character pointer and a long doubles Read the words from the included 'words2stxt' into a malloc’d bufer ‘words2’ while simultaneously flling an array of ‘wordentry’ elementss Perform an indirect bubblesort of ‘boringness’ on the words2 data using the contents of the wordentry array rather directly on the ‘words2’ input bufers


Haie your program stop when it has found the 1000 least boring words and haie it saie the frst thousand words of the sorted the result to the fle ‘Problem2_f1000stxt’s Haie your program print out the CPU tme used when it fnishess


Problem3/ qsortmain, helperfns.c, helperfns.h, Makefle


Create a ‘wordentry’ array for the ‘words’ bufer as in Problem 2s Perform a qsort of ‘boringness’ using the contents of the wordentry arrays
Saie the frst thousand words of the sorted the result to to a fle ‘Problem3_f1000stxt’s Haie your program print out the CPU tme used when it fnishess

Answered Same DayMar 03, 2021

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
151 Votes
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...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here