C++ Programming 3-7. Write a program that will use random number generation to c
ID: 3749758 • Letter: C
Question
C++ Programming
3-7. Write a program that will use random number generation to create sentences. Use the following four arrays to pick a word at random to form a grammatically correct sentence. The sentence will begin with an uppercase letter and end in a period. Use the following order from the following arrays: article, noun, verb, preposition, article, and noun.
article - "the", "a", "one", "some", "any"
noun - "boy", "girl", "dog", "town", "car"
verb - "drove", "jumped", "ran", "walked", "skipped"
preposition - "to", "from", "over", "under", "on"
The program is started below, fill in the for loop.
#include <iostream>
#include <cstring>
#include <ctime>
#include <cmath>
#include <cctype>
using namespace std;
void main() {
char article[5][5] = {"the", "a", "one", "some", "any"};
char noun[5][5] = {"boy", "girl", "dog", "town", "car"};
char verb[5][8] = {"drove", "jumped", "ran", "walked", "skipped"};
char preposition[5][6] = {"to", "from", "over", "under", "on"};
char sent[36];
int i;
srand(time(NULL));
for (i = 1; i <= 10; i++) {
/* use cstring functions to copy a random initial article, then use cstring functions to cat the other words onto the end of the sentence (in the above order). Also cat spaces between words and a period at the end of the sentence, along with making sure just the first letter of the sentence is uppercase. */
Explanation / Answer
// C++ code
#include <iostream>
#include <math.h>
#include <fstream>
#include <iomanip>
#include <stdlib.h>
#include <string.h>
using namespace std;
int main()
{
char article[5][5] = {"the", "a", "one", "some", "any"};
char noun[5][5] = {"boy", "girl", "dog", "town", "car"};
char verb[5][8] = {"drove", "jumped", "ran", "walked", "skipped"};
char preposition[5][6] = {"to", "from", "over", "under", "on"};
char sent[36];
char subsent[36] = "";
int i;
srand(time(NULL));
for (i = 1; i <= 10; i++)
{
// cstring functions to copy a random initial article
strcpy(subsent,article[rand()%5]);
strcat(subsent," ");
// cstring functions to cat the other words onto the end of the sentence (
strcat(subsent,noun[rand()%5]);
strcat(subsent," ");
strcat(subsent,verb[rand()%5]);
strcat(subsent," ");
// cat spaces between words and a period at the end of the sentence
strcat(subsent,preposition[rand()%5]);
strcat(subsent,". ");
// first letter of the sentence is uppercase
subsent[0]=toupper(subsent[0]);
strcat(sent, subsent);
}
cout<<sent<<endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.