Computer Science 101

Seventh Homework Assignment

Assigned: Week of November 4th
Due: Week of November 18th

Administrivia

This homework corresponds to the seventh lab assignment. It would probably be helpful to have completed that in advance! Be sure to read the homework policies in the syllabus before starting, and particularly before working together!

This assignment is larger than most. As a result, you have two weeks to get it finished. I strongly encourage you to get it done soon. There will be zero tolerance for late homeworks. If it is not submitted during lab in the week of November 18th, I will not grade it. In addition, since you have additional time, I expect the final program to be more polished. You should spend time reviewing your code, ensuring that it looks good. Your output should also look good.

Goals

The main goal of this assignment is to use arrays. It will help you cement your understanding of strings and arrays, including multidimensional arrays. At this point I expect you to be comfortable with conditionals, loops, and functions. You should have an understanding for what arrays are and how to use them. And you should have started to become clear about the distinction between pointer variables and "normal" variables and how both relate to arrays.

Assignment

The assignment is to write a random insult generator. The idea behind this handy tool is that occasionally you feel like insulting someone, but you do not feel clever enough to come up with an insult. That is where our tool comes in. Run the program and it will give you a guaranteed clever insult for you to hurl! Great fun for all.

Not all of you may feel the desire to insult your fellow students (or me), so feel free to write a random complement generator instead! Or a random sentence generator like the one here.

You can find an example insult generator here.

The way the insult generator works is to read a list of words from a file which it will use to generate the insult. It chooses words at random from this list, and glues them together. Depending on your algorithm and the words in your list, your insults will make more or less sense.

For this assignment you will need to write two functions. The first reads a list of words from a file, and returns them in an array. The second takes the list of words and returns a string which is the insult.

In order to generate the insult, you will need to choose words at random from your list. Two functions are helpful for this. The first is void srand( int ) which seeds the pseudo-random number generator. It is important that you call srand once in your program, otherwise the pseudo-random number generator will always return the same pseudo-random numbers. In the example below I seed the pseudo-random number generator with the current time so it always is different. The second function int rand( void ) which returns a random number. Very handy. Unfortunately, rand returns a number between 0 and a constant MAX_RAND which is about 32,000.

Your insults should be of different (random) lengths, so to illustrate the use of int rand() I will show you how to pick a random length. We want a number between 1 and 7 because we want at least one word and no more than 7 words in our insult. So we divide the result of rand by MAX_RAND to give us a number between 0 and 1. Now we can multiply this by 6 to get a number between 0 and 6, and then add 1 to get a number between 1 and 7.

#include 
#include 
#include 

void main( void )
{
   int insultLength;

   /* Seed the random-number generator with current time so that
    * the numbers will be different every time we run.
    */
   srand( ( unsigned ) time( NULL ) );

   insultLength = ( ( ( double ) rand() / RAND_MAX ) * 6 ) + 1;
}

We use casts in several places in this example. For instance we cast the result of time to be an "unsigned" integer (simply magnitude, no positive or negative). We also cast the result of rand to be a double. This forces the division to be floating point division and is very important. Without the cast the division would always return 0.

Going further

For many of you just getting a working random insult (or complement) generator will be enough. But for some of you the desire for it to make more sense will be too strong. There are several ways to make it produce more meaningful phrases or sentences. One is to have seperate lists for different parts of speech, and then glue the parts of speech together appropriately. This is how the Random Sentence Generator works. Another way is to choose your words carefully. Another one is to be careful to not repeat a word. There are many others. Feel free to push beyond the basic idea and try to make meaningful phrases or sentences!

Sample

I have written a simple and rather stupid insult generator. It often makes insults that mean nothing. But you can use it to see the idea. You will need a list of words such as this one.

Good Luck!!

Questions

  1. In your words, what was the main point of this assignment?








  2. What would you say the most valuable part of this assignment was (if any)?








  3. What would you say the least valuable part of this assignment was (if any)?








  4. How long did you spend on this assignment? Indicate where you spent the most time.