Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

My goal is to use system calls, to create a random phrase, print it to standard output. I am allowed to use the standard library

My goal is to use system calls, to create a random phrase, print it to standard output. I am allowed to use the standard library functions to generate random numbers. I was able to write my program in normal c without using system calls, but I am stuck/confused in how to use the read() system call to read the words from the files which have a single word on a single line. So I want to be able to use read() to read line by line and assign each word to an index in the array.

The random phrase will have a sentence structure like --> The 'adjective' 'adjective' 'noun' jumped over the 'adjective' 'noun', where the adjectives and nouns are the random words from the txt files.

What I have so far is this:

noun.txt contains: 5 4 Jack 3 Dog 3 Bob 4 Mike 5 Trump

adj.txt contains 6 3 red 4 loud 5 happy 5 quick 9 invidious 5 brown

line.c contains

#include

#include

#include

typedef struct

{

char *word;

} words;

int main() {

FILE *nounFile = fopen("noun.txt", "r");

FILE *adjFile = fopen("adj.txt", "r");

int nounCount;

int adjCount;

fscanf(nounFile, "%i", &nounCount); //store number of nouns to address of nounCount

fscanf(adjFile, "%i", &adjCount); //store number of adj to address of adjCount

words *nouns = malloc(sizeof(words) * nounCount); //allocate enough memory for number of nouns in file

words *adjs = malloc(sizeof(words) * adjCount); //allocate enough memory for number of nouns in file

for (int i = 0; i < nounCount; ++i)

{

int nounLen;

fscanf(nounFile, "%i", &nounLen); //read length of each noun to nounLen

nouns[i].word = malloc(sizeof(char) * (nounLen + 1)); //allocate enough memory for each noun

fscanf(nounFile, "%s", nouns[i].word); //store each noun in noun[i].word

}

for (int i = 0; i < adjCount; ++i)

{

int adjLen;

fscanf(adjFile, "%i", &adjLen); //read length of each noun to nounLen

adjs[i].word = malloc(sizeof(char) * (adjLen + 1)); //allocate enough memory for each noun

fscanf(adjFile, "%s", adjs[i].word); //store each noun in noun[i].word

}

srand (time(NULL));

printf("The %s %s %s jumped over the %s %s. ",

adjs[(rand()%adjCount)].word, adjs[(rand()%adjCount)].word,

nouns[(rand()%nounCount)].word, adjs[(rand()%adjCount)].word,

nouns[(rand()%nounCount)].word);

//clean up for noun

fclose(nounFile);

for (int i = 0; i < nounCount; ++i)

{

free(nouns[i].word); //clear up memory allocation of each noun

}

free(nouns);

//clean up for adj

fclose(adjFile);

for (int i = 0; i < adjCount; ++i)

{

free(adjs[i].word); //clear up memory allocation of each noun

}

free(adjs);

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Reliability Engineering Designing And Operating Resilient Database Systems

Authors: Laine Campbell, Charity Majors

1st Edition

978-1491925942

Students also viewed these Databases questions

Question

3 How supply and demand together determine market equilibrium.

Answered: 1 week ago

Question

What is Change Control and how does it operate?

Answered: 1 week ago

Question

How do Data Requirements relate to Functional Requirements?

Answered: 1 week ago