Throughout this lab, you'l be using files from the directory (on strauss) listed below:
/www/htdocs/CIS/181/sundaram/06S/work/labs/lab02
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 /www/htdocs/CIS/181/sundaram/06S/work/labs/lab02 .
cd lab02
Note: the "06S"
part contains a capital letter "S"
. A lowercase "s"
(as in "06s"
) will not work.
This will first change your current working directory to your cisc181 subdirectory, then copy the entire lab02
subdirectory from my account, along with all its contents, into the current directory. You'll then have to cd
into lab02
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 lab03 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 |