Throughout this lab, you'l be using files from the directory (on strauss) listed below:
~pconrad/public_html/cisc181h/05F/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/cisc181h/05F/work/labs/lab03 .
cd lab03
Note: the "05F" 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:
find . -name a.out | xargs /bin/rm 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.
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:
ls command, and note that there
is no file called outfile.txt in your directory.
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.
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.
rm command to delete output.txt from
your directory. Then make a script file lab03b.txt in
which you
ls to show that there is no output.txt file
in your directory,
ls command to show that output.txt was created,
output.txt.
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.
CC -o boxOfStars boxOfStars.cppNote that we use the
-o boxOfStars flag to name the executable boxOfStars instead of a.out
./boxOfStars
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.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.).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.| width | output |
| any number <= 0 | (no output at all) |
| 1 | * |
| 2 | * * * * |
| 3 | * * * * * * * * * |
| 4 | * * * * * * * * * * * * * * * * |
| etc... | |
| 8 | * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
| etc... |
| 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 |
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/lab03where userid is your strauss userid. Hence, the directory name is:
~/public_html/cisc181/lab03
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.
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.
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 |
set xrange [0:8] set yrange [0:8] set output "A.png" set terminal png large color plot "A.dat" with lines |
Here is an explanation of the gnuplot commands:
set xrange [0:8] signifies that the x-axis will run from 0 to 8. The set yrange command is similar.
set output "A.png" means that the output graphics will go to a file called "A.png". You could supply any other file here that you wish, though it should end with .png if you want it to work properly with your browser.
set terminal png large color means that we want .png output. (Note: There are other kinds of output that gnuplot can produce, but we won't need to know about those for this exercise.)
plot "A.dat" with lines means to get the data points from the file called "A.dat", and connect the dots with lines. There are other kinds of plots that gnuplot can do, but we won't need to know about those in this exercise.
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.
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.
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.
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.)
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.
Total: 100 points.