C++ Write a program that uses the random number generator to create sentences. T
ID: 3760514 • Letter: C
Question
C++
Write a program that uses the random number generator to create sentences. The program should use four arrays of pointers to char called article, noun, verb and preposition. The sentences are constructed in the following order: article, noun, verb, preposition, article, noun. The program should generate 20 sentences. Capitalize the first letter. The arrays should contain:
article "the", "one", "a", "some", "any"
noun "boy", "girl", "dog", "town", "car"
verb "drove", "jumped", "walked", "ran", "skipped" preposition "to", "from", "over", "under", "on"
Example Program Session:
Explanation / Answer
This program will generates the 20 sentence as per problem statement. It is using strcat function to append the words to gather as per the given fashion in the problem statement i.e is
order: article, noun, verb, preposition, article, noun.
It is using two dimentional charecter array for stroing the Resultanat of the data.
See the below C++ Program for solving ..
#include <cstdio>
#include <cstring>
#include <cctype>
#include <ctime>
#include <cstdlib>
using namespace std;
int main() {
const char* article[] = {"the" , "one" , "a" , "some" , "any" };
const char* noun[] = {"boy" , "girl" , "dog" , "town" , "car" };
const char* verb[] = {"drove", "jumped", "ran" , "walked", "skipped"};
const char* preposition[] = {"to" , "from" , "over", "under" , "on" };
char sentence[20][50] ;
srand(time(0));
int i;
for(i = 0; i < 20; i++)
{
strcat(sentence[i], article[ rand() % 5 ] );
strcat(sentence[i], " " );
strcat(sentence[i], noun[ rand() % 5 ] );
strcat(sentence[i], " " );
strcat(sentence[i], verb[ rand() % 5 ] );
strcat(sentence[i], " " );
strcat(sentence[i], preposition[ rand() % 5 ] );
strcat(sentence[i], " " );
strcat(sentence[i], article[ rand() % 5 ] );
strcat(sentence[i], " " );
strcat(sentence[i], noun[ rand() % 5 ] );
strcat(sentence[i], "." );
}
for(i = 0; i < 20; i++)
{
sentence[i][0] = toupper(sentence[i][0]);
printf(" %s ", sentence[i]);
}
return 1;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.