Lab04, CISC181 honors, Fall 2004

Files needed for this lab

The following files should be in the lab04 subdirectory. To find and copy these files into your account, see the detailed instructions below.

Copying files

The files for this lab are at http://udel.edu/~pconrad/cisc181h/04F/labs/lab04.
They can also be copied directly from ~pconrad/public_html/cisc181h/04F/labs/lab04.

From this point on in the semester, if I tell you the files for the lab are in the "lab04 directory in the usual place", you'll be expected to know from experience where to look.

If you forget where to look: Note that my website, http://udel.edu/~pconrad, has links to guide you to the web location of the files. You can "right click" in many web browsers to bring up the lab04 directory in its own window, so that you can see the full URL for the directory (the use of "frames" on my web page might "hide" that URL unless you do this.)

To convert a full URL into a filename or directory name on strauss:

(This is something you need to know for the exams.)
To convert "http://udel.edu/cisc181h/04F/labs/lab04" to a directory name:

  1. Take off the "http://udel.edu/" prefix
  2. Put "public_html" in between the ~userid part and the top level directory (in this case, cisc181h).
  3. This gives you ~pconrad/public_html/cisc181h/04F/labs/lab04, which is the directory you can copy the files from.

Comand you should know for the exam:

Hopefully the following commands are familiar to you. If not, you should be doing more reading in your Anderson textbook!

The command "cp -r ~pconrad/public_html/cisc181h/04F/labs/lab04 ." (don't forget the final dot and the space before it) will copy the entire directory into a lab04 subdirectory under your "current directory". Typically, that directory should be ~/cisc181 for a command of this form.

The command "ls ~pconrad/public_html/cisc181h/04F/labs/lab04" will give you a directory listing of that directory.

The command "cp ~pconrad/public_html/cisc181/04F/labs/lab04/file.foo ~/cisc181/lab04" will copy a single file from that directory into the directory ~/cisc181/lab04. If the target directory is your current directory, you can put "." in place of "~/cisc181/lab04"..

Part 1: Simple Nested Loops in C++

In this part of the lab, you will work with some simple nested loops in C++ that draw little boxes.

Please allow me to acknowledge right up front: these little "box drawing" programs are not particularly useful in and of themselves. In fact these programs are rather silly. So why are we doing them?

Consider that these programs are like "layup drills" in basketball, or "playing your scales" on a musical instrument; they re-enforce basic skills that will be useful later. Trust me: you are learning something valuable. Questions such as these also tend to show up on exams, so doing a few in lab is probably good practice. Finally, this is a warm up for an assignment that is definitely more useful: producing a table of wind chill factors.

  1. Compile the program boxOfStars.cpp using the command:
  2. CC -o boxOfStars boxOfStars.cpp
    Note that we use the -o boxOfStars flag to name the executable boxOfStars instead of a.out
  3. Run the program with the command:
    ./boxOfStars
  4. Now, use cat boxOfStars.cpp or use the text editor to look at the source code for this program and understand how it works. Look at the nesting of the for loops, and compare the for loops that start at 0 vs. those that start at 1. Also, compare the use of <= vs. the use of < in the tests on the for loops. Finally, look at the use of the mod 2 operations (%2) in the last loop in order to alternate between printing x and o.
  5. Now, take a look at boxFunctions.cpp. Compile it with a similar command. Understand how it works.
  6. Now you are ready to write your own program called lab04a.cpp. It should contain the following functions:
    1. A function called starField that writes a square pattern on cout like the one in the table below alternating between stars and spaces. The function should take one integer parameter called width, and return void. Here are sample outputs for various values of width (Note that your function should be prepared to take any integer as input; I'm just showing several examples to give you the idea of the pattern.).
    2. a function called outlineBox that returns void and takes height and width as integer parameters, and c as a character (char) parameter.. This function produces the outline of a box similar to those in the table of example output shown below.
    3. A main program that calls your two functions (several times each) and demonstrates that they produce the correct output for all the values listed in the two tables above.

    Here's the table that shows what the output of the starField function should look like:

    width output
    any number <= 0 (no output at all)
    1
     *
    
    2
     * *
    * *
    
    3
     * * *
    * * *
     * * *
    
    4
     * * * *
    * * * *
     * * * *
    * * * *
    
    etc...  
    8
     * * * * * * * *
    * * * * * * * *
     * * * * * * * *
    * * * * * * * *
     * * * * * * * *
    * * * * * * * *
     * * * * * * * *
    * * * * * * * *
    
    etc...  


    Here's the table showing what the output from the outlineBox function should look like:

    parameters (height, width, c)  
    3,4,'x'
    xxxx
    x  x
    xxxx	
    
    5,3,'x'
    xxx
    x x
    x x
    x x
    xxx	
    
    3,3,'o'
    ooo
    o o
    ooo	
    
    7,7,'*'
    *******
    *     *
    *     *
    *     *
    *     *
    *     *
    *******	
    
    2,7,'*'
    *******
    *******	
    
    7,2,'*'
    **
    **
    **
    **
    **
    **
    **
    
    7,1,'*'
    *
    *
    *
    *
    *
    *
    *
    
    1,7,'x'
    xxxxxxx	
    
    1,1,'x' x
    1,2,'x' xx
    1,3,'o' ooo
    Any input where height or width is less than or equal to zero (or both are less than or equal to zero) no output

  7. Now make a lab04a.txt script in which you list out, compile, and execute your lab04a.cpp program (in the usual fashion).

Part 2: Reading input from cin until cin.eof()

In this part of the lab, you'll learn how to read from cin (standard input) until the "end of file" is detected. From the keyboard, you signal end of file by typing "CTRL/D".

Later, you'll learn how to "redirect" standard input so that it comes from a file on the disk; at that point, the end of the file itself will mark the end of input.

  1. Copy the program readUsernames.cpp from the lab04 directory into your current directory.
  2. Open up the file with a text editor, and notice the structure of the code. What the program is doing is reading a series of usernames, and for each username that it reads in, it writes out a URL (Uniform Resource Locator, that is, a web address) that you can use to access that user's cisc181 web page.

    However, rather than reading until we hit a "sentinel" value, or until we've read a specific number of usernames, we use the cin.eof() function. This function returns "true" when we have hit the "end of file" on cin.

    When running an interactive program (one where a human user is interacting with the program, providing input and reading output), the user can signal the end of file by typing "CTRL/D". So, essentially, this program reads a line of input, and then repeateadly outputs URLs until the user puts in CTRL/D.
  3. Compile and run this program, placing your output in the file readUsernames1 (rather than a.out). If you are not sure how to do that, consult your Anderson book, the chapters on compiling C++ programs!
  4. Try "pconrad" as the userid. Then, try your own userid, and those of some of the classmates sitting near you in lab, or any other usernames that you remember of CISC181 students. (Oh, you aren't working on this in lab? Then make it a point to learn the usernames of some of your classmates next time you come to lecture or lab!) When you've typed in a few userids, hit CTRL/D to stop the program.
  5. You'll script this program as part of a later step, so you are done with Part 1. Go on to part 2 now.

Part 3: Using redirection ( < ) to read standard input (cin) from a file

  1. Now, copy the file "usernames.txt" into your directory from the lab04 subdirectory.
  2. Replace the list of usernames in this file with your own username, plust those of at least four additional current CISC181 classmates of yours (at least 5 names in all, but more if you like. No more than 15 for now though; don't want to waste paper in the script files!)
  3. Run the readUsernames program again, with the command line:

    ./readUsernames < usernames.txt

    This will cause the program to take its standard input (cin) from usernames.txt instead of from the keyboard. Notice that the output still contains all the prompts for the input, which is a bit messy.
  4. Now, copy the program makeHTML.cpp from the lab04 subdirectory. Edit it, and look over the source code. In this program, I've taken out all the prompts for input, and replaced all the output with "cout <<" statements that will produce HTML code. The result is that when you run the program with

    ./makeHTML < usernames.txt

    you will get as output something that is suitable to be the HTML code for a simple web page. You can store this into an HTML file via:

    ./makeHTML < usernames.txt > usernames.html

    The "> usernames.html" part of the command line is a shell feature that says "take the standard output (the cout stuff) and put it into a file called usernames.html". Use "cat usernames.html" to look at the contents of the usernames.html file.
  5. Now take your file "usernames.html", and copy it to a new subdirectory under your public_html directory, called:

    ~userid/public_html/cisc181/files/lab04/usernames.html

    and then browse it via http://copland.udel.edu/~userid/cisc181/files/lab04/usernames.html. (Replace "userid" with your username (e.g. pconrad). You'll need to do the "chmod"command, and use the proper "cp" command before your file will be available on the web. You will also need to use an "mkdir"command. . Check your old labs or your Anderson book for information on how to do all that. Then, browse the page you created, and check that the links work. If your own link doesn't work, then fix it!
  6. Make a script lab04b.txt in which you show the following steps:
    1. Compile readUsernames.
    2. Run readUsernames, inputting 3 usernames interactively, then typing CTRL/D.
    3. cat your usernames.txt file to show that you added a few extra names to it.
    4. Run readUsernames, redirecting the standard input from usernames.txt (as shown above).
    5. Compile and run makeHTML, redirecting the standard input from usernames.txt, and redirecting the output to usernames.html. (Note: you may have to delete the old copy of usernames.html to make this work properly.)

Finishing up and Submitting

  1. Upload all of the following files to WebCT and submit.
  2. Print the script files and give to your TA.

 

Grading Rubric

A "grading rubric" explains how an assignment will be graded. This is a "general" rubric. After reviewing your submissinons, your TA might develop a more specific rubric that explains individual deductions. You can get an idea in advance of what those deductions might look like by examing past rubrics; ask your TA for details.

Total: 100 points.