Lab03, CISC181, Spring 2005

Part 1: lab03a.txt, Copying files and checking your disk quota

Throughout this lab, you'l be using files from the directory (on strauss) listed below:

~pconrad/public_html/cisc181/05S/work/labs/lab03

You might want to just copy all the files from that subdirectory into your current directory. Here's a sequence of commands that will both create a new subdirectory for lab03, and copy all the files you will need into it:

cd ~/cisc181
cp -r ~pconrad/public_html/cisc181/05S/work/labs/lab03 .
cd lab03

Note: the "05S" part contains a capital letter "S". A lowercase "s" (as in "05s") will not work.

This will first change your current working directory to your cisc181 subdirectory, then copy the entire lab03 subdirectory from my account, along with all its contents, into the current directory. You'll then have to cd into lab03 to see all the files. The -r stands for recursive, and the "." at the end of the second command is a symbol meaning "the current directory is the target of the copy command".

(Note: This command might just show up on an exam, so be sure you understand how it works!)

In addition, you should take this opportunity to check your disk quota on strauss.

Type "quota -v" at the Unix prompt. You'll see some numbers such as the following:

> quota -v
Disk quotas for pconrad (uid 53430):
Filesystem     usage  quota  limit    timeleft  files  quota  limit    timeleft
/scratch           0      1      1                  0      0      0            
/home/usra     12944  20240  20240               3417  76800  76800            
> 

You need to compare the number listed under "usage" with the number listed under "quota". For example, in the listing above, the usage is 12944 (somewhere been 12MB and 13MB) and the quota is 20240 (20MB). This is good; this is about where you want to be.

If on the other hand, your usage is very close to your quota, you may run into lots of difficulties. Compling, editing, logging on and off of the Sun Ray servers—all of these operations may begin to fail or produce strange errors.

In this case, you need to clean up your disk space usage. Here are some useful commands which you should learn (and know for the next exam!)

> cd
> du -sh *

This sequence of commands changes directory to your main directory, and then gives you a list of all your files and directories in your main directory, along with a summary of the disk usage for each file or directory. The -s flag indicates that you will get a summary of all the disk space under a particular directory, while the -h flag indicates that you will get results reported in K or M, standing for Kilobytes (units of 1024 bytes) or Megabytes (units of a bit over a million bytes, 1024 x 1024 to be exact).

A good candidate for deleting is your old "executable" files; a.out files, and any file you create with the -o flag from the compiler. They tend to be large, and once you've produced your script, you don't need them anymore; you can always bring them back any time you like by recompiling. But DON'T get rid of the source files (.cpp, .cc, .h. .dat, etc.) for your old labs; you may need them for future labs this semester! Only delete the exectuable files, and the .o files (ask your TA if you are not sure.)

Other things to know about disk quota:

To turn in: As part of your script for part 2 of this lab, you'll run the "quota -v" command and show that your disk usage is at least 3-4MB less than your quota. That's the amount of "headroom" you need in order to effectively do programming work. If your usage is higher than that, use the "du -sh *" command to find where the trouble is, then start doing some deleting.

You do NOT have to script the deleting part; only the "quota -v" command showing the end result of your clean up needs to be in your script. That "quota -v" command will show up in a later step of this lab. For now, provided you've done your cleaning up, you are all finished.

Part 2: lab03b.txt, Writing to an output file with ofstream

Sometimes the output from one program forms the input to another program. For example, we might use one program to generate a series of data points, and then use a plotting program such as gnuplot to visualize the output as a graph. In this case, it is convenient to be able to write the output of a program directly to a file.

We know that with statements such as "cout >> x", we can write to standard output, which is usually the screen. We can "redirect" standard output to a file using Unix commands (and we might learn how to do that in a different lab this semester). However, it is sometime more convenient to be able to send output directly to a specific file. That way we can send some output to the screen, and some output to a file. This also allows us to have multiple output files (say one output file for data points, and a different output file for a gnuplot script).


The "ofstream" object allows us to write to an output file directly from inside your program.

To learn about ofstream objects, do the following steps:

  1. Compile the ofstreamDemo.cc file (copied in Part 1 of this lab) but don't run it yet.
  2. Before you run it, do an ls command, and note that there is no file called outfile.txt in your directory.
  3. Now run the program. Note the output you get on your screen. Then do another ls command and note that there should now be a file called outfile.txt in your directory. Use cat outfile.txt to list the output of this file.
  4. Now edit the ofStreamDemo.cc file to personalize the message that is going to the file. Make it print your name to the file as part of the message, along with any other pithy comments you want to pass along to your TA and/or instructor.
  5. Use the rm command to delete output.txt from your directory. Then make a script file lab03b.txt in which you
    1. run the "quota -v" command to show that you are 3-4MB below your disk quota (see step 1 of this lab)
    2. cd into your ~/cisc181/lab03 directory
    3. cat and then compile your new (personalized) version of ofStreamDemo.cc,
    4. use ls to show that there is no output.txt file in your directory,
    5. run the program,
    6. redo the ls command to show that output.txt was created,
    7. and cat the output of your new personalized output.txt.
  6. Now, upload your lab03b.txt file to WebCT and print it. You do not need to upload the other files to WebCT.

Part 3: 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 in lab04 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 lab03c.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 lab03c.txt script in which you list out, compile, and execute your lab03.cpp program (in the usual fashion).

 

Part 4: Drawing letters with the gnuplot program

Background

In this exercise, you will use a program called gnuplot to draw letters of the alphabet. This exercise, together with some future labs, illustrates some of the basic ideas behind how "fonts" work in systems such as Microsoft Windows, Macintosh OS, and the X Windows system used on strauss.

This exercise will begin to teach you what you need to know about gnuplot.

In this exercise, you will first use a text editor (vi or emacs) to create a file that contains points that draw a letter of the alphabet. You will then use gnuplot to plot those points, and store the resulting graphic in an output file. The output file has the extension .png, which stands for "Portable Network Graphics". Files ending in .png are similar to files ending in .gif, .jpg, or .jpeg, except they use a different format for representing the pixels in the image, and for compressing those pixels.

This exercise also assumes you know how to put files on the web under your public_html directory, use the chmod command to make those files readable, and point your web browser at those files.

The suggested link for putting the files you create with this exercise is:

   http://copland.udel.edu/~userid/cisc181/lab03
where userid is your strauss userid. Hence, the directory name is:
   ~/public_html/cisc181/lab03

Keep your public_html files and your regular lab files separate

Note that you should NOT put C++ code in a directory under your ~/public_html/cisc181/lab03 directory. That code goes into ~/cisc181/lab03, which is a different directory.

Make special note of the difference. Points may be deducted if you end up with your C++ files visible on your web page.

Why are we learning about gnuplot in a C++ programming course?

Gnuplot offers us an easy way to generate graphics from C++ programs. In future labs, we'll be generating data files using C++ programs, and then using Gnuplot to visualize that data.

Creating a gnuplot data file: "A.dat"

A typical gnuplot data file consists of lines of text, where on each line there are two numbers, representing an (x,y) coordinate. Here is a gnuplot data file called "A.dat", followed by an explanation of its contents:

# A

0 0
2 8
4 0

1 4
3 4
Explanation:

A file containing gnuplot commands: "A.gnuplot"

To plot this data file, you use a file that contains gnuplot commands. Here is an example file "A.gnuplot" that takes "A.dat" as input, and produces an output file "A.png". The actual graphic produced appears after the listing of A.gnuplot:

set xrange [0:8]
set yrange [0:8]
set output "A.png"
set terminal png large color
plot "A.dat" with lines

A.png graphic

Here is an explanation of the gnuplot commands:

Note: if you want to learn more about gnuplot commands, you can read about these by finding the gnuplot documentation on the web, or looking at the online help inside of the gnuplot program itself. The commands listed above are probably sufficient for this exercise, however.

Actually plotting the graphic: creating the "A.png" file

To actually create the "A.png" file using "A.dat" and "A.gnuplot" (see above), type the Unix command:
gnuplot A.gnuplot

Then, to see the file, copy it into a directory on your webpage (i.e. somewhere under your ~/public_html directory tree) and do the "chmod" command that makes it readable. You should then be able to point your web browser to the file and see the output.

Sometimes setting the xrange and yrange commands in the gnuplot file to [-1,8] works better than [0,8]; when you go from -1, the xaxis and yaxis don't cover up part of your letter, and its sometimes easier to see what is going on. Experiment with changing the value from 0 to 1 to see what happens when you repeat the gnuplot command, and copy the resulting .png file to your webpage and refresh.

Another example: CIS.dat, CIS.gnuplot, CIS.png

You will also see another set of three files. The CIS.dat file and CIS.gnuplot files can be used to produce a CIS.png file. However, note that if you look at the CIS.png file (by pointing a web browser to that file) the S in CIS is incomplete. As an exercise, see if you can modify the points in CIS.dat to make the S complete, and get the correct graphic to come up. That will help check your understanding of how gnuplot works before you go to the next step.

Finally, there is a similar set of files: V.dat, V.gnuplot and V.png if you want to see one more example.

Writing your own data file and gnuplot file

Create a data file called initials.dat that contains the points needed to print your first and last initial. Create each using block capital letters. Put your first initial in a box where the lower left corner is at position 0,0, and the upper right hand corner is at position 4,8. Put your second initial in a box where the lower left hand corner is at position 5,0, and the upper right is at position 9,8. That will leave one "point" of space between the two capital letters.

Also create a initials.gnuplot file that will take the input from initials.dat and produce an output file called initials.png. Adjust the xrange and yrange in your gnuplot file appropriately.

Grading and Submission

For this part of lab03, you do not have to submit anything to WebCT. Instead, just make a directory under your web page called ~/public_html/cisc181/lab03, and copy your initials.dat, initials.gnuplot and initials.png files (from step 5) into that directory. Make the directory and all the files inside it readable. Your TA will look in that directory for the finished work. (You do not have to submit anything for any of the others steps in this lab.)

Final Thoughts

Writing a .dat file and a .gnuplot file by hand can be tedious. If we are going to plot a large graphic, we might want to write a program to do it instead. Next week in lab, we'll do just that.

 

Finishing up and Submitting

  1. Make sure you have a lab03b.txt and lab03c.txt script files, per instructions above.
    (There is no lab03a.txt file this week.)
  2. Upload lab03b.txt, lab03c.txt and lab03c.cpp file to WebCT and submit.
  3. Print the two script files and give to your TA.
  4. Make sure you can see initials.dat, initials.gnuplot and initials.png when you go to http://copland.udel.edu/~userid/cisc181/lab03 (where userid is replaced with your userid).

Grading

Total: 100 points.