CS140 -- Lab 2

This lab comes in five parts. The first two do not require that you use malloc(), while the latter three require that you use malloc(). Dr. Plank has provided sample executables for you in /home/plank/cs140/Fall-2004/labs/lab2/answers so if you ever have a question about how your program should perform on a certain set of inputs, try running the appropriate executable from Dr. Plank's directory.

Part 1: Maxmin

Your job here is to take an input file composed of names and scores, and to print out the maximum and minimum numbers in the input file. The specific format of the input file is as follows. Each line is of the form:

name score

The name may contain any number of words with any amount of white space between them. No word in a name may be a number. The score is a floating point number (use a double). Example input files are input1, input2 and input3 in the lab2 directory.

Maxmin should take an input file on standard input, and print out the maximum and minimum score. If standard input is not in the proper form, maxmin can do anything.. You should try out the example executable in /home/plank/cs140/Fall-2004/labs/lab2/answers:

UNIX> maxmin < input1
Max: 0.714000
Min: 0.377000
UNIX> maxmin < input2
Max: 68.430000
Min: 35.900000
UNIX> maxmin < input3
Max: 74.580000
Min: 69.210000

You should also try this on other input files that you make up. Does your executable work if the input file has no lines?

You may assume that no line in your input file is greater than 1000 characters. Words may be any size up to 1000 characters.

(Hint: use scanf("%s", ...) to read in words, and then use sscanf(s, "%lf", ...) to see if it is a score.)

Part 2: Maxminname

Maxminname works just like maxmin, but it also prints the names with the maximum and minimum scores. Each word in the name should be printed with one space between each word. If more than one line contains the maximum or minimum score, then print ``MULTIPLE-NAME'' as the name. Again, if you have further questions about what maxminname should do, test the executable in /home/plank/cs140/Fall-2004/labs/lab2/answers:
UNIX> maxminname < input1
Max: 0.714000 New York Yankees
Min: 0.377000 Detroit
UNIX> maxminname < input2
Max: 68.430000 Ted McLellan & Brad Vander Zanden
Min: 35.900000 MULTIPLE-NAME
UNIX> maxminname < input3
Max: 74.580000 Chip Beck
Min: 69.210000 David Duval

Hint: you should have four character arrays in your program; all of them should be 1000 characters:

You read words into word, and build name. When you get a score, see if you should update max (and therefore maxname) and/or min (and therefore minname). When you reach the end of standard input, use max, maxname, min and minname to make your final printout.

Part 3: PGM negatives

A PGM file is a kind of digital picture. You can view pgm files with the xv program (or, if that does not work, then try the instructions here). You can find the following pgm files in ~cs140/www-home/spring-2005/labs/lab2: Pgm files are nice because you can actually view them both as pictures with xv, and as text -- go ahead and look at one of them with more, vi or whatever editor you use.

A picture is simply a two-dimensional array of pixels. In pgm files, a pixel is a grey value between 0 and 255. Zero is black, and 255 is white. Everything in between is grey.

Pgm files have a specific format. (Actually, I've tweaked them a little to make this easier for you). It is composed of words. The first word is ``P2''. Then the next two words define the number of pixels in the picture -- first is the number of columns in the two-dimensional array, and then the number of rows. The next word is the number 255. So, if you look at the beginning of jsp.pgm, you'll see:

UNIX> head jsp.pgm
252 267
 37  44  37  37  44  37  44  44  44  37  44  44  48  48  48  44  37 
 34  33  34  37  33  34  37  48  44  44  44  48  44  44  44  44  53 
 44  44  37  44  53  53  49  44  41  37  41  41  33  33  29  26  26 
 29  29  33  37  37  44  48  53  53  57  53  57  61  64  69  69  81 
 81  77  81  81  73  77  72  48  37  33  33  33  37  37  48  44  33 
 22  22  25  22  22  14  14  14  14  22  22  22  22  22  22  22  22 
 22  22  26  22  22  22  22  18  22  18  22  18  18  18  22  26  22 
This means that jsp.pgm is a 267 * 252 array of pixels. After the 255 come all the pixels. First, the top row, then the next row, etc. Note that the ASCII formatting of the file doesn't mean anything -- there could be one pixel per line and the file would still be a legal pgm file. In jsp.pgm above, the first 252 pixels are those in the top row, then the next 252 are in the second row, and so on. There will be a total of 267*252 = 67284 pixels. After the last pixel, the file ends.

Before you go any further, create a PGM file of your own -- make it 10x10 and give the pixels any value you want. Take a look at it with display. Cool, no?

Now, your first pgm program should take a pgm file on standard input, and create a pgm file on standard output that is the negative of the input file. Call this program neg.

Note, you don't need to call malloc() with this program. Just read in the first four words, print them out, then print out 255 minus each pixel. You can put a newline after each pixel if you want to make it really easy (this is what I did). Check out the output, and see how it looks:

UNIX> neg < jsp.pgm > jspneg.pgm
UNIX> display jspneg.pgm

Part 4: Hflip

Hflip reads a pgm file on standard input, and prints a pgm file on standard output. The output file should be the horizontal reflection of the input file -- in other words, left is right and right is left.

You'll have to use malloc() for this one. Read in the first four words, and then malloc() a row's worth of pixels. Then, for each row, read in the row and write it out backwards. Test it out!

Part 5: Vflip

Vflip reads a pgm file on standard input, and prints a pgm file on standard output. The output file should be the vertical reflection of the input file -- in other words, up is down and down is up.

You'll have to do a little more now. Allocate an array of row (int *)'s. Then initialize each one of those (int *)'s by calling malloc() to allocate an array of column ints. Read each row of pixels into one of these arrays, and then print them out backwards.

If you want to have more fun with pgm files and practice more programming

Here are more things to write. These are optional and will not be graded, but if you want more practice, have fun with them:


There is a makefile that you can use in ~cs140/www-home/spring-2005/labs/lab2/makefile.

When you're done, clean up all your stray pgm files -- they are large and with 60 students, that's a lot of wasted disk space. If you want to keep the images, use display to turn them into gif or jpg images to save space.