Assignment 08

Due: Friday, Mar. 23, 2012 at 12:00 p.m. 100 pts.

For this assignment you will submit (in the usual way) multiple files. These files will contain a C++ program that you have named appropriately, each file with an appropriate name and appropriate extension as well as input files to your program.

This program is to give you practice using:

    • functions

    • arrays

    • loops

    • null-terminated character arrays and strings

    • the system's random number generator

    • file I/O

    • everything else we've covered up till now

Background Dr. Nick's sorting algorithm is helping him out of so many jams that he has now turned his attention to other occupations. He's now spending his time on more, well, shall we say, worthwhile endeavors. He's writing poetry! But, he has found that writing poems is pretty easy because he seems to be able to write almost anything and there's always someone who thinks it's good (see picture at right).

So, in this assignment you are to help Dr. Nick by writing a poetry generator. Modern poetry does not always rhyme. In fact, Dr. Nick does not even know what a rhyme is.

Specifications: Before you continue with this assignment, take a look at an example on-line poetry generator. Run this poem generator a few times to see how it works. Your generator will be similar to this example, only all lists of words as well as the poem sentence-patterns will be placed in separate files. Your program is to prompt the user for the number of lines the user wants in a poem, and generate the requested number of poetic lines using pre-specified sentence-patterns and lists of words. Allow the user to continue generating new poems as long as the number of requested lines is greater than zero. To generate your poems, you will use sentence-patterns specified in a file patterns.dat. Each sentence-pattern in the patterns file is defined as a list of integers and words, where

    • 1 is a noun

    • 2 is an adjective

    • 3 is a verb

    • 4 is a pronoun

    • 5 is an adverb

    • 6 is an interjection (oh, ah, gawwwll-leee!)

and words are words/punctuation to be output just as they appear in the pattern. The end of the sentence-pattern is designated with -1. An example of a sentence-pattern is

6 why does 2 1 3 ? -1

which corresponds to a line of poetry of the form:

<interjection> why does <adjective> <noun> <verb> ? e.g. oh my why does bouncy dog flood -James Joyce, I think

You will have separate files containing lists of words for each kind of word. Thus, you will have a file containing just nouns, a file containing just adverbs, etc. Hence, you will have a total of seven input files (See Attachments). For each line of a poem you are generating, you will need to read in a randomly selected sentence-pattern from the patterns file. Then for each word type in your sentence-pattern, you will need to read a randomly selected word from the appropriate file. Output your poem to the screen. Some of the questions you will face while writing this program are:

    1. How do I represent a sentence-pattern in my program?

    2. How do I map each word type in a sentence-pattern to the appropriate file name?

    3. How do I read a randomly selected sentence-pattern or word from a file?

Here are some answers to some unrelated questions:

    1. π

    2. 67.8

    3. Lake Michigan

Here are some answers that will help you with this assignment:

    1. A sentence-pattern is just a sequence of chars which you can store in an array (or not) and "evaluate" any way you deem appropriate. Remember, -1 indicates the end of the sentence-pattern and does not correspond to any word type.

    2. There are many ways to map the integers to the appropriate file names. But how you implement this will depend on how you read the data in the patterns file. Don't underestimate how hard this might be to deal with.

    3. To randomly select a sentence-pattern/word from a file you first need to know how many sentence-patterns/words are in that file. To make this easier, the first entry in each file is an integer indicating how many sentence-patterns/words that file contains. So if you need to randomly select a word from a file containing 10 words, use the random number generator to produce a random integer between 1 and 10. Suppose your random integer is 7, then skip over the first 6 words and read the 7th word. To skip over the first 6 words you can simply read them but not use them. Make sure you account for the first entry in a file being the number of actual sentence-patterns/words that file contains. Note that each time you want to read a random word from a file, you need to connect that file to an input stream, read your word, and then disconnect the file from the input stream (close it). If you don't do this, bad things will happen.

When you submit:

    1. generate an 8-line poem

    2. being horrified with the outcome, generate a 3-line poem.

    3. then quit in disgust!

As always, if you have questions about this or any other assignment, be sure to ask your instructor or the tutor-person. (I think the pony-tail at the top is a dead giveaway.)