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

3-7. Write a program that will use random number generation to create sentences.

ID: 3907446 • Letter: 3

Question

3-7. Write a program that will use random number generation to create sentences. Use the following tour 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 fron the following arrays: artice, noun, verb, preposition, article, and noun. Run your program in the console window, copy and paste the results, and save in ex3-7.bd as a plain text file. Attach your ex3 7.cpp program file and your ex3-7.txt output text file, and submit. article "the", "a", "one", "some", "any noun "boy", g "dog", toum", "car verb "drove" jumped", "ran", walked", "skipped preposition - To", Trom", "over. "under", on The program is started below, fill in the for loop. Bill Wohlleber Exercise 3-7 Thia program il1 generate random sentencea #include.cstring> # 1 nclude # include u ing name space std; void main) char article [5][5] {"the", "a", "one", "some", "any"); char noun [5][5] = {"boy", "girl", "dog", "town". "car"); char verb[5 [9]"drove", "1umped", "rar", "alkeá" "skipped"; char prepoaition [5] [6]"to""from", "over", "under", "on" char gent 136]i int ii srand (time (NULL) ; * 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. V

Explanation / Answer

Solution:

sentence.cpp

#include <iostream>
#include<cstring>
#include <cctype>
#include<cmath>
#include<ctime>
#include<stdlib.h>
#include<fstream>

using namespace std;

int main(){


//Declaration and definition of variables
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));

ofstream file;
file.open ("C:/sentence.txt"); //Specifying the path to store the sentences.
//If specified file is not present then it will create for you.

//Here 10 is the number of sentence
for(i = 1; i<=10; i++){
sent[0] = ''; //It separates the sentences and does not delete previous sent

strcat(sent, article[rand()%5]);
strcat(sent, " "); //It replacing the last character of the sentence except null

strcat(sent, noun[rand()%5]); //It looks for the noun and concatenate with the previous word
strcat(sent, " ");

strcat(sent, verb[rand()%5]); //It looks for a verb and concatenate with the previous word
strcat(sent, " ");

strcat(sent, preposition[rand()%5]); //It looks for a preposition and concatenate with the previous word
strcat(sent, " ");

strcat(sent, article[rand()%5]); //It looks for an article and concatenate with the previous word
strcat(sent, " ");

strcat(sent, noun[rand()%5]); //It looks for another noun and concatenate with the previous word
strcat(sent, ".");

sent[0] = toupper(sent[0]); //Capitalizing the first letter in the sentence.

file<<sent<<endl;//Writing each sentence into the file named sentence.txt.


}

file.close();//Closing the file stream


}

The output of sentence.txt file:

One boy walked from a car.
A town drove over some town.
A boy walked from a car.
A car skipped under some town.
A car ran on any car.
A boy drove from a boy.
Any town drove under a boy.
A girl skipped under a dog.
One boy skipped on the boy.
Some car skipped from a dog.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote