C programming II

In this lab, we'll look at little at the C I/O package and the traditional C "IDE", the make command.

This lab is also to get you started on the first lab assignment. Since this lab is only meeting one hour a week, you do need to do some out-of-lab work.

Getting Ready

Download the DistLab.zip file and unzip it into your csci/255 directory. This should create a subdirectory DistLab with five files. Use the editor look at each of these files.

The five files

The include file point.h defines a C structure called point2d. C structures are like Java classes without methods. This is the standard way of aggregating heterogeneous data in C.

The include file distance.h defines prototypes for two C functions distance and distancesquared. Prototypes give the names of functions along with the types of their arguments. The * in the argument list specifies these functions receive pointers as arguments.

The C program file distance.c implements the C functions distance and distancesquared. Note the -> operator in the distancesquared function. When a C structure is passed with a pointer reference, this operator is used to access the structure's fields.

The distance function uses C's sqrt function. Go the the command line and look at the man page for sqrt.

[yourid@yourmach currentdir] man sqrt

The man page not only tells you the types of arguments sqrt is passed and the type of value it returns, but is also states that your C program must include math.h and be linked with the option -lm.

The C program file dplot.c contains the main routine for your application. It contains calls to fputs to write strings to the terminal. There are also many calls to fprintf, C's most important output function which Java has "borrowed" for its own formatting print routines. There is also a single call to scanf to read a number from the terminal.

You could use the on-line man pages to look at these functions, but you'd be better of looking at the appropriate sections of the C Programming/File IO section of Wikibooks.

The final file Makefile is used by the make program to make it easy for you to compile your program. The College of Engineering at the University of Hawaii has a pretty good make tutorial, but for now it might be best just to accept this Makefile as a gift.

Compiling and running your program

Connect to your csci/255/DistLab. Next compile your two C files and link them with a single make command. You can then run the executable dplot which will ask you to input a single number.

[yourid@yourmach currentdir] cd ~/csci/255/DistLab
[yourid@yourmach DistLab] make
[yourid@yourmach DistLab] ./dplot

Your ultimate task

Your ultimate task is to complete Lab Assignment 1. However, during this lab you'll just get started.

Your task for today

At the least add code to read in the y variable for the one point used to compute all distances. Then try your hand at making a nice row and column plot of the distances, even if your distances are all bogus.