Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PROGRAMMING LANGUAGE C You are now allowed to use the following in additional to

ID: 3700616 • Letter: P

Question

PROGRAMMING LANGUAGE C

You are now allowed to use the following in additional to the techniques of the previous chapters:

• pointers

• pointer operators

• pass by reference simulation

• const qualifiers

• size of operator

•function pointers

•character handling library

• string handling library

• string conversion functions

• additional input/output functions from: getchar() , fgets() , putchar() , puts() , sprintf() , sscanf()

W Assignments: COP3223 ) Assignments: COP322- Homework-08-SP ng C How to Program eChegg Study Guided x-e PROGRAMMING LANG 1 ? 0 × ? x x × ? | file:///C/Users/Valena%20Moctezuma/Downloads/Homework-08-Spring-2018-1%2011.pdf Homework-08-Spring-2018-1 (1)-pdf 213 Q2: (Random Sentences) (25 points) Write a program that uses random number generation to create sentences. The program should use four arrays of pointers to char called article, noun, verb and preposition. The program should create a sentence by selecting a word at random from each array in the following order: article, noun, verb, preposition, article and noun. As each word is picked, it should be concatenated to the previous words in an array large enough to hold the entire sentence. The words should be separated by spaces. When the final sentence is output, it should start with a capital letter and end with a period. The program should generate 20 such sentences. The arrays should be filled as follows: The article array should contain the articles "the", "a", "one", "some" and "any"; the noun array should contain the nouns "cat", "dog", "truck", "plane" and "skateboard"; the verb array should contain the verbs "drove", "jumped", "ran", "walked" and "flew"; the preposition array should contain the prepositions "to", "from", "over", "under" and "on" S:01 PM O Type here to search

Explanation / Answer

Code:


#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

// Driver code
int main()
{
  
srand(time(0));
int i,j;
int randArr[20][6];
for(j=0;j<20;j++){
for (i = 0; i < 6; i++) {
int num = (rand() -1) % (5);
randArr[j][i] = num; //random array containing all the random values for all sentences
//printf("%d ", randArr[j][i]);
}
//printf(" ");
}
//given arrays
char article[][20] = {"the","a","one","some","any"};
char noun[][20] = {"cat","dog","truck","plane","skateboard"};
char verb[][20] = {"drove","jumped","ran","walked","flew"};
char preposition[][20] = {"to","from","over","under","on"};
char twentyScentence[120][20]; //120 words (20 sentences*6 words each) with each word length <20
  
int count = 0;
for(j=0;j<20;j++){
strcpy(twentyScentence[count],article[randArr[j][0]]);
count++;
strcpy(twentyScentence[count],noun[randArr[j][1]]);
count++;
strcpy(twentyScentence[count],verb[randArr[j][2]]);
count++;
strcpy(twentyScentence[count],preposition[randArr[j][3]]);
count++;
strcpy(twentyScentence[count],article[randArr[j][4]]);
count++;
strcpy(twentyScentence[count],noun[randArr[j][5]]);
count++;
}
for(i=0;i<120;i++){
if(i%6 == 0){
twentyScentence[i][0] = toupper(twentyScentence[i][0]);
}
printf("%s ",twentyScentence[i]);
if((i+1)%6 == 0) printf(". ");
}
return 0;
}

Output:

The truck drove from one cat .
One dog jumped on the plane .
Any truck ran over the dog .
The dog ran from any skateboard .
Any skateboard jumped on some dog .
A skateboard ran over a cat .
A skateboard flew under a cat .
Any skateboard drove to one truck .
One plane jumped over one dog .
One cat flew to the cat .
Any skateboard ran over some skateboard .
The truck jumped from some dog .
Any cat walked from a dog .
A dog ran to the plane .
Any cat flew from some truck .
One skateboard walked to any skateboard .
One dog ran over the plane .
A truck flew to a skateboard .
One cat walked over a skateboard .
A skateboard walked under a truck .


This code is working perfectly fine in the given constraint and generating a perfect result. I've also added comments, wherever possible.
Hope this helps.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote