Lab03, CISC181 honors, Fall 2004

Assigned in Lecture, Tuesday 9/21/2004. Due by the end of lab (3:20pm) 9/27/04 (the day of this coming Monday's lab).

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

Throughout this lab, you'l be using files from the lab03 subdirectory. 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/04F/labs/lab03 .
cd lab03

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.)

To turn in: a script file lab03a.txt in which you 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 do 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. You should submit that script to WebCT and print a copy to turn in to your TA.

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.

For Part1 of this lab, 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. cd into your ~/cisc181/lab03 directory
    2. cat and then compile your new (personalized) version of ofStreamDemo.cc,
    3. use ls to show that there is no output.txt file in your directory,
    4. run the program,
    5. redo the ls command to show that output.txt was created,
    6. 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: Generating a five pointed star.

In a previous lab, you have seen how to use gnuplot to plot points that you entered by hand using emacs or vi to create a data file (e.g. initials.dat).

Now, you will write a program, lab03c.cpp, to produce a data file containing a five pointed star. Eventually, we want the program to be able to also produce a 6 pointed star, or 7 pointed star, etc.

The star will be contained within a circle centered at the origin, and with a radius of 10 units.

The "top" point of the start will be at the point (0,10), ten units above the origin, right on the y axis. The other four points will be at angles of 360/5 degrees around the circle. You can use trig to determine the x,y coordinates of these points (sin(), cos(), etc.) Consult your Deitel/Deitel text on details of using #include <cmath> to access these functions (remembering to use radians rather than degrees).

You'll open an external file inside the C++ program (with ofstream). Call it "star.dat". Then write out the points to this file. Two nested "for loops" should do the trick. The pseudocode is as follows:

 

for each of five points of the circle (call this point p)
   {
   for each of the four points other than p (call this point q)
       {
         write out a line connecting p to q
         write a blank line in the gnuplot file
           (so gnuplot lifts the pen)
       }
   }

When you have finished writing the points, close the output file. You can generate the gnuplot script (star.gnuplot) either by hand editing it in vi or emacs. Even better would be to open it also as an output file, and write it out. Set the xrange and yrange appropriately, e.g. [-10,+10] or [-11,+11] so that the entire star appears in the output. Use star.png as the output file. (Writing it from the program allows you to make the number "10" a constant or a variable that can be modified later to make different size stars. The number of points, 5, is also a number that should be a constant or a #define so that it can be modified later.

What to turn in:

Finishing up and Submitting

  1. Make sure you have a lab03a.txt and lab03b script files, per instructions above. Upload these two script files to WebCT.
  2. Upload lab03c.txt and a lab03c.cpp file to WebCT.. You do not need to upload star.dat, star.gnuplot, or star.png.
  3. Submit lab03a.txt, lab03b.txt, lab03c.txt and lab03.cpp to WebCT.
  4. Make sure your star.png file is readable on your web site under the URL http://udel.edu/~userid/cisc181/lab03/star.png.
  5. Print the three script files and give to your TA.

Grading

Total: 100 points.