Question

Assignment #6 - Arrays and Strings - Making random sentences! Due: Tuesday April 2, 11:59:00 pm...

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

  • Write a program that uses random-number generation to create sentences. Create four arrays of strings (string objects highly suggested over c-strings) called article, noun, verb, and preposition.
  • The arrays of Strings:
    • The article array should be const and contain the articles: "the", "a", "one", "some", "any"
    • The preposition array should be const and contain the prepositions: "to", "from", "over", "under", "on"
    • The noun array and the verb array should contain words entered by the user. So start the program by asking the user to enter 5 nouns (each seperated by a space -- this should give you a hint as to how to read them in), and then ask them to enter 5 verbs (again, each seperated by a space). Store these in the appropriate arrays.
    • Ensure that all the words stored in the noun array and the verb array are stored as all lowercase words. The user could enter a word with any case they choose, but it's up to you to store them as lowercase strings in your array. You may assume the user will enter valid verbs whem prompted and valid nouns when prompted.
  • Create a sentence by selecting a word at random from each array in the following order:
      article, noun, verb, preposition, article, noun 
    
    This 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.
  • 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 end with a period. Your program should generate random 20 sentences and output them to the console.
  • Note: It's not enough to output just one word at a time. You need to create each sentence as a single string before printing it to output. (Make use of the concatenation operation! String objects make this easy!)
  • Hint: The hardest part is making sure the sentence is capitalized, because you only have one article array (and it stores the lowercase version, since you use articles again). Remember that we've seen a number of methods from the string class, that help with this. You may want to remind yourself of these:
    • In the cctype library, we saw functions that can check for upper or lowercase characters and even some that can change values from uppercase to lowercase and vice versa.
    • From string class, at, [] notation, and the + operation will also be helpful.
  • Functions in addition to main() are not required but it may be a good idea for you to create some if you find yourself needing to do a similar task over and over ...

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

  • No global variables
  • You may use any of these libaries:
    • iostream
    • iomanip
    • cctype
    • string
    • cstring
    • cstdlib
    • ctime
  • Readable and well-documented source code
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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";
   }

}

============================================================================================

Add a comment
Know the answer?
Add Answer to:
Assignment #6 - Arrays and Strings - Making random sentences! Due: Tuesday April 2, 11:59:00 pm...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a script that uses random number generation to create sentences. Use four arrays of strings...

    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...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT