CS 211 - Lab 4

Fall 2012

Due: Monday, September 24th, 2012 at 11:59 pm

Random Sentence Generator

For this lab, you are to write a C program that will create random sentences. No not expect these sentences to make much sense, but you never know what might get generated.

These sentences will be made up from words in the following files:

These files contain words that correspond to the "part of speech" indicated by the name of the file. The files contain one "word" per line. Note that a "word" may contain multiple white space delimited words (as as we defined in lab 3). One such example is the noun "bus driver". Extra white space characters in the middle of the "word" should be kept, but extra white space characters before or after the "word" should be removed. These files are also available on the CS Linux machines in the ~i211/lab4 directory. You can view the contents of this directory by using the following command.
ls ~i211/lab4
You can view the contents of the files using the more or cat Unix commands, such as
more ~i211/lab4/nouns
You can copy these files to your current directory if you are on the CS Linux machines using the command:
cp ~i211/lab4/* .
These sentences are created by using one of the following sentence forms:
<article> <adjective>* <noun> <verb> .
<article> <adjective>* <noun> <verb> <article> <adjective>* <noun> .
Your program should randomly pick one of the above two forms with a 50% probabilty of each form being picked.

Your program should substitute each item in the sentence form with a word from the corresponding file. I.E. the <noun> item will be substituted with one word from the nouns file. Each word in the file should have the same probability to be selected. The substituted items in each sentence should be separated by only one space and all sentences are to end with a period.

The only special case is the <adjective>* item. The asterisk implies that zero or more adjectives may be used in the sentence. The probability of the number of adjectives will be as follows:

  • zero adjectives - 20%
  • one adjective - 40%
  • two adjectives - 20%
  • three adjectives - 10%
  • four adjectives - 5%
  • five adjectives - 2%
  • six adjectives - 2%
  • seven adjectives - 1%
You may assume that the files are found in the current directory when running the program.

Your program is to allow for an optional command line argument. This argument must be a numeric value greater than zero. This argument indicates the number of random sentences that are to be created. If this optional command line argument is not given, create only 1 sentence. The created sentences are to be output to standard output.

For this program, you will need to use the rand() and srand() library functions. These functions are part of the stdlib.h library. Note that proper use of srand() requires the use of time(). The rand() function will generate a random number from zero to RAND_MAX. While RAND_MAX may vary from machine to machine, it will always be at least 32,767, which will work just fine for this program. Using the modulus operator, %, with the result from rand() will generate a values in a desired range with "equal" probability (or at least with equal enough probability for the sake of this lab).

For purposes of this program you may assume that each "word" from the file is at most 40 characters long. Also, you may assume that each file contains at most 500 words.

Program Submission

Your are to submit the programs for this lab via the Assignments Page in Blackboard.

To help the TA, name your file with your net-id and the assignment name, like:

  • ptroy1Lab4.c

Submit this file via the Assignment Link for the Lab in Blackboard.

  1. In the CS 211 Web Pages in Blackboard, go to the Assignments Page
  2. Click on the link for the correct lab. This will open a web page with the title: "Upload Assignment: Lab X", where X is the number of the lab
  3. Scroll down and click on the button "Browse for Local File"
  4. Select the file that you created that contains the program. Then click OK.
  5. Repeat steps 3 and 4 for your second program.
  6. Click the submit button on the "Upload Assignment: Lab X" page.
  7. You should see the Submission History page that shows what you submitted. Verify you actually submitted the correct information.
-- Main.troy - 2012-09-19
Topic attachments
I Attachment Action Size Date Who Comment
Unknown file formatEXT adjectives manage 0.7 K 2012-09-19 - 13:52 UnknownUser  
Unknown file formatEXT articles manage 0.1 K 2012-09-19 - 13:52 UnknownUser  
Unknown file formatEXT nouns manage 0.1 K 2012-09-19 - 13:52 UnknownUser  
Unknown file formatEXT verbs manage 0.3 K 2012-09-19 - 13:52 UnknownUser  
Topic revision: r1 - 2012-09-19 - 14:36:53 - Main.troy
Copyright 2016 The Board of Trustees
of the University of Illinois.webmaster@cs.uic.edu
Helping Women Faculty Advance
Funded by NSF