CS 12 Homepage
Due via e-turnin by Saturday 4/10, 11pm
Collaboration Policy:
Collaboration on home programming assignments is strictly FORBIDDEN.
Programs must represent YOUR OWN original work.
Sharing code or team-coding are not allowed. Copying code from ANY source
(any book, current or past students, past solutions, the web, etc.) is not allowed.
Cooperation to the extent of helping to debug, or discussing the general
approach to solving the problem is encouraged, but should never involve
communicating code or even pseudo-code or explicit algorithms. Your code must be unique.
Imagine, if you will, that you wanted to have your computer generate truly atrocious haiku
poetry. A haiku is a Japanese poem with no rhyme scheme or meter composed of three lines:
one of 5 syllables, followed by one of 7 syllables, ended by one of 5. How would you do it?
One way to do it is to generate 7 lists of words corresponding to words with
1, 2, 3, 4, 5, 6, or 7 syllables respectively. Then have 7 helper functions which (randomly)
decide whether to return 1 word of the appropriate length, or call two smaller helpers which
produce the same total number of syllables.
This is a bit complicated. Let us walk through an example:
- We want to generate a 5 syllable line. We randomly choose a number between 1 and 5
to represent the number of syllables the first word on our line will be.
- We choose 3
- We randomly choose a word from our 3 syllable word list.
- We still have 2 syllables, which could be either 2 words of one syllable each,
or 1 word of two syllables. We call our helper that generates two syllables
to finish off.
- The two syllable helper has two options available to it. It chooses (randomly) that
we will have a 2 syllable word. We pick a word from our 2 syllable word list.
- Finally, we return the two chosen words
A similar technique can be used to break things down into word types (noun, verb, adjective, etc)
to generate simple sentences.You could even go further to have helpers that generated "subjects"
(either a noun or an adjective and a noun, or several adjectives and a noun, etc). This technique
is a simplification of a general idea known as a "context-free grammar", and has a strong influence
in computational theory as well as modern linguistics.
Specs
In this assignment, you will create a program that generates random sentences or poems given a
pattern (grammar) of your creation. You will write a series of helper functions that return values
of type string, many of which will want to call helpers of their own.
Your program must have no fewer than 7 such helpers with an average of 4 possibilities in each
helper. Your subject matter can be whatever you like. Bonus credit is available for those that have
many rules and produce more natural-seeming output.
A simple example follows:
string bobsName()
{
if (rand() % 2 == 0)
return "Bob";
else
return "Mr. Smith";
}
string greeting()
{
int num = rand() % 3;
if (num == 0)
return "Hello";
else if (num == 1)
return "Greetings";
else
return "Welcome";
}
string sayHi()
{ if (rand() % 2 == 0)
return greeting();
else
return greeting() + ", " + bobsName();
}
Turn this assignment in as "sentences.cc".
© 2004 UCRiverside
Department ofComputer Science & Engineering.
All rightsreserved.