Question
For this problem you will teach your computer to recite haiku, an ancient form of Japanese poetry that is characterized by its three lines with
For this problem 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: 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.
Squeeze trigger? (yes/no)
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.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started