CISC181, Lab04, Fall 2004

Part 0: Copying files

Files for this lab come from the subdirectory:

~pconrad/public_html/cisc181/04F/labs/lab04

To get started, you should copy all the files from that subdirectory into your own ~/cisc181/lab04 directory.


Note: Previous labs cover the commands for creating a new ~/cisc181/lab04 subdirectory and copying all the files from my lab04 subdirectory into your subdirectory, so refer to those labs if you are not sure what to do at this step. Next lab I'll just give you the name of the directory where can find the files and tell you to copy them into your own directory.

Part 1: Windchill table

The file multTable.cpp illustrates an example of a program that uses nested for loops to produce a multiplication table similar to the one you might see hanging in a 3rd or 4th grade classroom. I want to suggest that you compile and run this program, and try to understand the source code as a preliminary step to this assignment.

Your task is to write a program called lab04a.cc, which will produce a table of wind chill factors. The formula that you should use is given on the web site: http://www.nws.noaa.gov/om/windchill/index.shtml (see the formula at the bottom of the table.)

You are only going to produce a subset of that table; here is an example of what your output should look like:

> CC lab04a.cc -o windChill
> ./windChill
           Temperature (degrees F)        
------------+-------+-------+-------+-------+-------+-------+
 wind (mph) |    40 |    30 |    20 |    10 |     0 |   -10 | 
------------+-------+-------+-------+-------+-------+-------+
         10 |    34 |    21 |     9 |    -4 |   -16 |   -28 | 
         20 |    30 |    17 |     4 |    -9 |   -22 |   -35 | 
         30 |    28 |    15 |     1 |   -12 |   -26 |   -39 | 
         40 |    27 |    13 |    -1 |   -15 |   -29 |   -43 | 
         50 |    26 |    12 |    -3 |   -17 |   -31 |   -45 | 
> 

Your program should include a function as follows:

double windChill(int windMph, int tempF)
{
   return ... // you fill in the rest
}

You will call that function inside two nested for loops that print the table; one iterates over windMph, and the other over tempF. For example, for temperature, you might use a for loop such as: for (w = 10; w<=50; w+=10)

Once you are satisfied with this step, modify your program in the following way:

  1. Change the line
  2. int main(void)

    at the top of your program to

    int main(int argc, char *argv[])
  3. Add #include <cstdlib> to the #includes at the top of your program.
  4. Add the following code in your main:

    // initialize from command line arguments; default to 10, 50 and 10 int windMin, windMax, windStep; windMin = (argc <= 1)? 10 : atoi(argv[1]); windMax = (argc <= 2)? 50 : atoi(argv[2]); windStep = (argc <= 3)? 10 : atoi(argv[3]);
  5. Now make sure your program still compiles, and still works just as it did before.
  6. Finally, change your program so that the values of windMin, windMax,and windStep are used to control the loop that chooses the values for wind (down the left hand column). You should find that if your run your program with the command ./windChill 10 50 10 for example, you will get the same table as before. However, you should now be able to run the program with commands such as the following:
    ./windChill same output as before
    ./windChill 5 60 5 values 5, 10, 15, 20, 25 ... up to 60 down the left column
    (just like in the table on the web site.)
    ./windChill 2 10 1 values 2, 3, 4, 5, 6, 7, 8, 9 and 10 (8 rows of wind chill factors)
    ./windChill 20 40 values 20 30 40 only (the step value defaults to 10)
    ./windChill 30 values 30 40 50 only (max defaults to 50, step defaults to 10)
  7. Test your program on all the possible values for the command line arguments in the above table.
  8. Now make a script lab04a.txt in which you cat your lab04a.cc file, compile it, and run it, showing the output for all the possible values of the command line arguments in the previous table. You will upload this script to WebCT along with your lab04a.cc file, and also give a printed copy of the script file to your TA.

Part 2: Drawing letters with Gnuplot

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/lab04
where userid is your strauss userid. Hence, the directory name is:
   ~/public_html/cisc181/lab04

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.

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 lab04, you do not have to submit anything to WebCT. Instead, just make a directory under your web page called ~/public_html/cisc181/lab04, 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.

Submission Instructions

Grading