CISC105 Spring 2005 Lab08

A Different Type of Lab

We've been writing a lot of programs and creating files. Now, it's time to kick things up a notch and use C to generate data files for another application, gnuplot.

Part 0: Creating your own web page

Goal 1: Creating a personal web page

  1. If you already have a personal web page on strauss (one that comes up when you type http://udel.edu/~userid, where userid is your UDelNet ID), then skip to the next goal (creating your CISC105 web page). Otherwise, cd into your ~/public_html directory.

  2. Use emacs to create a file in that special directory. The file must be named "index.html". The contents of that file should be something like the following:

    <html>
      <head>
        <title>Joe Sample's Web page</title>
      </head>
    
      <body>
        <h1>Joe Sample's Web page</h1>
    
        I had to do this <b>web page</b> for my <a
       href="http://udel.edu/~pconrad/cisc105">CISC105 class</a>.
       Right now it is pretty lame, but I hope to make it better later.
    
      </body>
    </html>
        

    Here's what that looks like once it is formatted by a web browser:

    Joe Sample's web page

    I had to do this web page for my CISC105 class. Right now it is pretty lame, but I hope to make it better later.

  3. After you save the file, you need to type in the following command:
    chmod a+rx ~/public_html/*

    This command makes all files and directories in your public_html directory accessible to all users on strauss, as well as all users on the World Wide Web. You need to repeat this command each time you add new files under public_html that you want users of the Web to be able to access.

    More details:The asterisk in the above command matches every file name in the public_html directory. You can also use chmod on individual files and directories as shown in class. You need to repeat this command each time you add new files under public_html that you want users of the Web to be able to access.

  4. To see that it works, type into a web browser:
    http://copland.udel.edu/~userid

    If it doesn't come up, make sure you set all your file permissions correctly. The most common problem is not doing the "chmod a+rx ~/public_html/*" command. If you did that, and it still doesn't work, check with your TA or with a classmate for help.

Goal 2: Creating a CISC105 web page

  1. cd into ~/public_html/cisc105. Now, create an index.html file in this directory containing html code similar to the personal page.
  2. Now, add an image to your web page. Find an image on the Web (or from wherever you want) and copy it into your public_html/cisc105 directory. Then, put the image in your CISC105 Web page, using the HTML tag <img src="imagename.jpg">, where "imagename.jpg" is the (case-sensitive) name of your file.
  3. After you have created the index.html file and/or copied your image file into your public_html/cisc105 subdirectory, you may need to repeat the chmod a+r [filename] command to get the web page to come up when you use the URL http://udel.edu/~jsample/cisc105.

Part 1: Using gnuplot

Goal 1: Learning gnuplot

In this exercise, you will use a program called gnuplot to draw bar graphs. This exercise illustrates how you can use C with other applications.

You will first use a text editor to create a file that contains the size of the bars to draw. You will then use gnuplot to plot the bars 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 link where you must put the files you create with this exercise is

   http://copland.udel.edu/~userid/cisc105/lab08
where userid is your strauss userid. Hence, the directory name is
   ~/public_html/cisc105/lab05

Creating a gnuplot data file: "bars.dat"

A typical gnuplot data file consists of lines of text, where on each line there are two numbers, representing an x-value and a y-value. Here is a gnuplot data file called "bars.dat", followed by an explanation of its contents:

# number of days in each month of 2005

1 31
2 28
3 31
4 30
5 31
6 30
7 31
8 31
9 30
10 31
11 30
12 31

Explanation:

A file containing gnuplot commands: "bars.gnuplot"

To plot the "bars.dat" data file, you use a file that contains gnuplot commands. Here is an example file "bars.gnuplot" that takes "bars.dat" as input and produces an output file "bars.png". The graphic has an xrange of 0 to 13 so that all 12 months will appear and a yrange of 0 to 32. The "plot" command says to use "bars.dat" as the input file and plot the first column (1) as the x-value and the second column (2) as the y-value. The actual graphic produced appears after the listing of bars.gnuplot:

set terminal png color large
set output "bars.png"
set data style boxes
set boxwidth 0.4 
set xtics nomirror
set border 11
set xrange [0:13]
set yrange [0:32]
set xlabel "Months" 
set ylabel "Days in Month"

plot 'bars.dat' using 1:2 title "Num Days"

Part 2: How random is rand()?

We want to see just how random "rand()" is. To do this, we will generate random numbers from 0 to 9. If rand() is random, then your function to create random numbers will generate equal amounts of each number (0 to 9).

Program 0: Generate frequency counts

Create an array that maintains the number of times each number is generated by rand(), i.e., an array that maintains the frequency count of each number. Generate 10 random numbers between 0 and 9. Count how many times each number is generated and store it in the array. Print out the contents of the array.

Repeat the process for 100, 1000, and 10000 random numbers and print out the results. (Does the array size need to change?)

Program 1: Generate gnuplot data files

Now, generate four gnuplot data files. The x-value is the generated number, and the y-value is the frequency count of each generated number. Include comments in your data file about its contents.

Start by generating the file for 10 generated random numbers. After you have worked the bugs out, generate the data files for 100, 1000, and 10000 random numbers as well.

Plotting the output

Modify bars.gnuplot to plot the frequency counts. Label the axes appropriately. Generate 4 graphs, one for each of the four data files that you generated.

Discussion

In your C files, comment on how random "rand()" is. How does randomness change with more iterations?

Part 3: Posting your files

Copy your .png files into your web-accessible lab08 directory. (Do not put your C code or data files in this directory.) Create an HTML file that includes each of your graphs. Clearly label each graph. Use a browser to check that your file is viewable by clicking in your directory. Getting the files viewable and in the right place is an important part of this lab.

Submit your two C programs, four gnuplot, four data, and four png files to WebCT. Submit a printed script file of your C programs, compiled and executed to your TA. Then, cat each data file. Your TA will also grade your web pages and the generated graphs.


Parts of this lab were borrowed from Phill Conrad's Fall 04 Lab01 and Lab05 labs.