Problem(programing C) Generate random sentences ( a modified version from a book
ID: 3831870 • Letter: P
Question
Problem(programing C)
Generate random sentences ( a modified version from a book Deital and Deital starting with C )
Your program will generate sentences using four arrays : article, noun, verb , preposition. Each word is picked from the arrays in the following order - Article, noun_1, verb, preposition, article and noun_2.
define four arrays :
char article [ ] [ 6 ] = { "the", "some" , "That" } ;
char noun_1 [ ] [ 6 ] = { "boy", "girl", "dog" } ;
char verb [ ] [ 7 ] = { "drove", "ran", "barked" } ;
char preposition [ ] [ 6 ] = { "to", "at", "over", "into" } ;
char noun_2 [ ] [ 6 ] = { "ditch", "house", "bridge", "city" } ;
A article won't appear twice in the same sentence.
The first letter in the sentence is upper case.
There is a space between each word.
There is a period at the end of each sentence.
Generate at least five sentences.
Explanation / Answer
This program will generate random sentences based on above requirement.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#include <string.h>
/* Definition of function for sentence generation */
void sentenceGenerator(char article[], char noun[],
char verb[],char preposition[], char noun_2[]);
/* main program */
int main()
{
int i = 0;
char article[6] = { "the", "some" , "That" } ;
char noun[6] = { "boy", "girl", "dog" };
char verb[7] = { "drove", "ran", "barked" } ;
char preposition[6] = { "to", "at", "over", "into" } ;
char noun_2[ 6 ] = { "ditch", "house", "bridge", "city" } ;
while(i<=7){
sentenceGenerator( article, noun, verb, preposition , noun_2);
i++;
}
system ("pause");
return 0; /* Indicates successful termination */
}
/* Function Prototype */
void sentenceGenerator(char article[], char noun[],
char verb[],char preposition[], char noun_2[])
{
char sentence[20];
sprintf("%s %s %s %s %s %s.",article [srand(time(0)) % 5],noun [srand(time(0)) % 5],verb [srand(time(0)) % 5],preposition [srand(time(0)) % 5],noun_2 [srand(time(0)) % 5]);
sentence[0] = toupper( sentence[0] );
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.