Assignment #6 - Arrays and Strings - Making random sentences!
Due: Tuesday April 2, 11:59:00 pm
Objective
This assignment will consist of writing a program that involves practice with arrays, random number generation, and Strings. You may use c-strings or string objects here, however, string objects is recommended.
Exercise
Filename: sentences.cpp
article, noun, verb, preposition, article, nounThis means, you need to generate a random value for each array to grab a word. Example: You cannot generate one random value (say, 1) and then take index 1 of each array. You must generate a random value to use as the index in each array you'll get a word from.
Sample Run 1
(user input is underlined)
Enter 5 nouns (lowercase): truck CAR dog MAN boy Enter 5 past tense verbs (lowercase): ran jumped killed attacked cried Some truck cried from the truck. One boy jumped under the car. Any car jumped from some man. A car ran from one truck. A truck attacked on a dog. The man attacked on some boy. One boy killed over any car. A truck killed under the dog. A car killed from a man. Some dog attacked on the truck. The man killed on one boy. One car ran on the man. The truck attacked from the dog. A man killed on any man. Any dog attacked over one truck. One car ran on one boy. A car killed under any car. Some man attacked on a boy. The boy cried under any dog. The man jumped from one dog.
Sample Run 2
(user input is underlined)
Enter 5 nouns (lowercase): book chair platypus villain clock Enter 5 past tense verbs (lowercase): shot FELL slapped YeLLeD cooked The chair slapped from some platypus. The platypus shot from one chair. Some villain yelled under any platypus. Any platypus shot from some platypus. The book yelled under the platypus. Any clock shot from any clock. The book yelled from the platypus. A book cooked on one book. A clock slapped on some platypus. A platypus yelled over any platypus. One book slapped over one book. Any platypus cooked over the chair. One clock slapped under a chair. The book cooked to one book. A chair yelled under the chair. Any book yelled over any book. One book slapped on some chair. A villain fell to a villain. One villain slapped on the book. The platypus cooked on some book.
Requirements for each program
Thanks for the question.
Here is the completed code for this problem. Comments are
included so that you understand whats going on. Let me know if you
have any doubts or if you need anything to change.
Thank You !!
========================================================================================
#include<string>
#include<cstdlib>
#include <ctime>
#include<iostream>
using namespace std;
const int MAX_COUNT=5;
const string articles[] = {"the","a","one","some","any"};
const string prepositions[] =
{"to","from","over","under","on"};
// takes in a string reference
// converts all alphabets to lower case
// returns nothing
void toLowerCase(string &word){
for(int i=0; i<word.size();i++){
if('A'<=word.at(i) &&
word.at(i)<='Z'){
word.at(i) = 'a'
+(word.at(i)- 'A');
}
}
}
// converts to propercase
string toProperCase(string article){
string properCaseWord = string(article);
properCaseWord.at(0)='A' + (properCaseWord.at(0)-
'a');
return properCaseWord;
}
int main(){
string nouns[MAX_COUNT], verbs[MAX_COUNT];
cout<<"Enter 5 nouns (lowercase): ";
for(int i=0;
i<MAX_COUNT;i++)cin>>nouns[i];
cout<<"Enter 5 past tense verbs (lowercase):
";
for(int i=0;
i<MAX_COUNT;i++)cin>>verbs[i];
for(int i=0; i<MAX_COUNT;i++){
toLowerCase(nouns[i]);
toLowerCase(verbs[i]);
}
int random=0;
for(int sentence=0; sentence<=20;
sentence++){
random = rand()%MAX_COUNT;
cout<<toProperCase(articles[rand()%MAX_COUNT])<<"
"
<<nouns[rand()%MAX_COUNT]<<" "
<<verbs[rand()%MAX_COUNT]<<" "
<<prepositions[rand()%MAX_COUNT]<<" "
<<articles[rand()%MAX_COUNT]<<" "
<<nouns[rand()%MAX_COUNT]<<"\n";
}
}
============================================================================================

Assignment #6 - Arrays and Strings - Making random sentences! Due: Tuesday April 2, 11:59:00 pm...
Write a script that uses random number generation to create sentences. Use four arrays of strings called article, noun, verb and preposition. 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, concatenate it to the previous words in the sentence. The words should be separated by spaces. When the final sentence is output, it should start with a capital letter and...