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:
#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 asrand()
, and time operations respectively.using namespace std;
: This allows the use of standard identifiers without requiring thestd::
prefix.string getWord(string[]);
andvoid printSentence(string[]);
: These lines are function prototypes for two functions that will be defined later in the code.srand( time(NULL) );
: This seeds the random number generator with the current time, ensuring that the numbers generated byrand()
will be different each time the program is run.Arrays of string variables (
articles
,nouns
,verbs
, andprepositions
) are declared and initialized with certain words. There is also a two-dimensional string arraysentences
declared to store 20 sentences, each made up of 6 words.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 functiongetWord()
is used here to select a word at random from a given array.The second loop iterates over each of the generated sentences and prints them to the console using the
printSentence()
function.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.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