For this assignment you will teach your computer to recite haiku, an ancient form of Japanese poetry that is characterized by its three lines with a fixed number of syllables in each. You will do this by having your program read in a file that contains a large number of sample haikus. It will then “remix” them by selecting a random line from three different haikus to produce a new one.

Your program will be given an input file like the following (but longer):

  The beauty of C
  Is its freedom and power,
  But beauty can hurt.

  I learned all I know
  In Software Engineering.
  Give Prof. Wood a raise.

  Oh what have I done?
  I started the homework late.
  The (seg) fault is mine.

Note that haikus have a specific format: the first line has five syllables, the second has seven, and the last also has five. You must read in the contents of the file, storing all of the five syllable lines into one array and all of the seven syllable lines into another (thus you should expect twice as many entries for the five syllable line array). You must two arrays of strings (i.e., a char*[]) to store the set of short and long lines. You also should only be allocating enough space at each pointer for the actual length of the string. Thus for the first haiku above, you would malloc 16, 26, and 21 bytes for the first three lines respectively (including the \0).

Once you have read in all the lines of the file and stored them into the proper arrays, you will use a random number generator to pick a set of three lines to print to the screen. For example, given the inputs above, you might produce this remix:

  Oh what have I done?
  In Software Engineering.
  The beauty of C

Or more likely you will get something even more nonsensical, but it fits the 5-7-5 pattern, so it is a valid haiku!

Requirements

A complete program will perform the following:

  • Read in an input file named haiku.txt
  • Store the lines into two char*[] data structures, one for short lines, one for long
  • Use the int rand() function declared in stdlib.h to pick three random numbers
  • Use those random numbers as indexes into your two arrays to produce a new haiku
  • Repeat this process to print a second haiku
  • Then, set a new seed for your random number generator by calling srand (time(NULL))
  • Generate and print 10 random haiku

It should also:

  • Print out the total number of haiku read in from the input file
  • Print out the total amount of memory reserved using malloc to store all of the haiku lines

You can make the following assumptions:

  • The input file is formatted so the first line of the file is the first line of the first haiku
  • Every haiku is three lines
  • There is an empty line between every haiku
  • The file contains at most 256 haiku in total
  • The longest line of any haiku is at most 128 characters

Below is a sample input file: (the git repo includes a longer example).
Note, I did not write these haiku. They were found at these two websites:

  • http://www.matthewpico.com/haikus/
  • http://www.funny2.com/haiku.htm

I can not vouch for their poetic beauty or creativity.

Rather than beep
Or a rude error message:
These words: "File Not Found".

Errors have occurred.
We won't tell you where or why -
Lazy programmers!

Chaos reigns within.
Reflect, repent, and reboot
Order will return.

For a new PC,
Center of my universe,
I abandon all.

The code was willing!
It considered your request,
But the chips were weak.

Everything is gone.
Your life's work has been destroyed.
Thank god for GitHub.

Short Answer

In addition to writing the program described above, you must answer the following questions:

  • How much memory does your program use in total to store all of the haiku lines? Hint: Be careful and think about both the stack and the heap! How much would it consume if you used 2D arrays sized based on the criteria listed in the assumptions above? Put your answers in a comment block at the top of your C file.
  • Write your own haiku on the topic of C programming or memory management. If you have trouble counting syllables, try using this (awesome) website: writeahaiku. Put your haiku in a file named readme.txt and include it in your git submission.

Resources

You should take a look at this page for a list of common mistakes in C as well as a pointer to more useful resources. You should DEFINITELY read either the “Practical C Programming Book” or the “Essential C” document linked to from that page. You may need to learn on your own about the functions in the string.h header–you will need to use those to find the length of a string and copy it between different areas. You’ll also have to learn how rand() works.

Grading

Grading Rubric: (10 pts max)

   -2 points if code does not compile
   -1 point if missing name at top of file
   +5 points if meets all listed requirements
   +2 points if correctly prints heap memory usage and haiku count
   +2 points if answers all short answer questions correctly
   +1 point for clean, well organized code

(partial credit is possible in each category)