Wrapping it all up - Multi-Dimensional Array of Strings

Program creates random sentences from arrays of words

// Description: End of arrays module, example uses // multiple arrays, and passing arrays to functions. // The program will create random sentences #include<iostream> #include<string> #include<cstdlib> #include<ctime> using namespace std; //Function prototypes // function to get a word from an array of 5 words string getWord(string[]); // function to print a sentence from an array of words void printSentence(string[]); int main() { // seed the random number generator srand( time(NULL) ); // declare local variables string articles[] = {"the", "a", "one", "some", "any"}; string nouns[] = {"boy", "girl", "dog", "town", "car"}; string verbs[] = {"drove", "jumped", "ran", "walked", "skipped"}; string prepositions[] = {"to", "from", "over", "under", "on"}; string sentences[20][6]; // array to hold 20 sentences of 6 words // for each of the 20 sentences, select the 6 words for(int index = 0; index < 20; index++) { // get each of the 6 words sentences[index][0] = getWord(articles); sentences[index][1] = getWord(nouns); sentences[index][2] = getWord(verbs); sentences[index][3] = getWord(prepositions); sentences[index][4] = getWord(articles); sentences[index][5] = getWord(nouns); } // print each of the 20 sentences for(int index = 0; index < 20; index++) printSentence(sentences[index]); return 0; } // Function Implementation // function to get a word from an array of 5 words string getWord(string wordList[]) { // declare local variables int randomNumber = rand() % 5; // return the word at the index of the random number return wordList[randomNumber]; } // function to print a sentence from an array of words void printSentence(string sentence[]) { // for each of the 6 words in the array, print the word for(int word = 0; word < 6; word++) { // capitalize the 1st letter of the 1st word in sentence sentence[0][0] = toupper(sentence[0][0]); cout << " " << sentence[word]; } cout << ".\n"; // period & new line at the end of the sentence }

 

This C++ program generates 20 random sentences, each containing 6 words, and prints them. The sentences are constructed using random selections of words from predefined arrays, specifically arrays of articles, nouns, verbs, and prepositions.

Here's a breakdown of the code:

  1. #include<iostream>, #include<string>, #include<cstdlib>, #include<ctime>: These lines include the standard C++ libraries for input/output operations, string operations, and the standard library for general purpose functions such as rand(), and time operations respectively.

  2. using namespace std;: This allows the use of standard identifiers without requiring the std:: prefix.

  3. string getWord(string[]); and void printSentence(string[]);: These lines are function prototypes for two functions that will be defined later in the code.

  4. srand( time(NULL) );: This seeds the random number generator with the current time, ensuring that the numbers generated by rand() will be different each time the program is run.

  5. Arrays of string variables (articles, nouns, verbs, and prepositions) are declared and initialized with certain words. There is also a two-dimensional string array sentences declared to store 20 sentences, each made up of 6 words.

  6. The program uses two loops. The first loop generates the sentences by randomly choosing words from the arrays and storing them in the sentences array. The function getWord() is used here to select a word at random from a given array.

  7. The second loop iterates over each of the generated sentences and prints them to the console using the printSentence() function.

  8. getWord(string wordList[]) is a function that takes an array of strings as an argument, generates a random number between 0 and 4, and returns the word in the array at that index.

  9. printSentence(string sentence[]) is a function that takes an array of strings as an argument, representing a sentence. It capitalizes the first letter of the first word of the sentence and prints out each word in the sentence, followed by a period and a new line.

In conclusion, this program is a random sentence generator, using predefined words from specific categories (articles, nouns, verbs, prepositions), and demonstrates usage of arrays, functions, random numbers, and simple text operations.

COSC-1336 / ITSE-1302 Computer Science - Author: Dr. Kevin Roark