Lab02, CISC181h, Spring 2006

Part 1: Copying files and checking your disk quota

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

/www/htdocs/CIS/181h/pconrad/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/181h/pconrad/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:

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: lab02a.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/lab02 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 lab02a.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 lab03 that is definitely more useful: producing a table of wind chill factors.

  1. Compile the program boxOfStars.cpp using the command:
    CC -o boxOfStars boxOfStars.cpp
    Note that we use the -o boxOfStars flag to name the executable boxOfStars instead of a.out
  2. Run the program with the command:
    ./boxOfStars
  3. 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.
  4. Now, take a look at boxFunctions.cpp. Compile it with a similar command. Understand how it works.
  5. Now you are ready to write your own program called lab02b.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

  6. Now make a lab02b.txt script in which you list out (cat), compile, and execute your lab02b.cpp program. Ask your TA for help if you are unclear about what is expected here.

 

Finishing up and Submitting

  1. Make sure you have two script files, as explained above
  2. Upload three files to WebCT and submit:
  3. Print the two script files and give to your TA. Be sure to staple them together and write your name on them, as well as "lab02" somewhere on the front. Neatness and clarity of your output will affect your grade.

Grading

 


Valid XHTML 1.1 Valid CSS!