Lab04 CISC105, Fall 2004, P. Conrad

Acknowledgements

This lab is a collaboration between Phill Conrad and Terry Harvey.

Part 0: Copying files

Under your cisc105 subdirectory, make a new directory for lab04, just like we did for lab04 last week (see last week's lab if you are not sure what to do).

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 lab04, and copy all the files you will need into it:

 

cd ~/cisc105
cp -r ~pconrad/public_html/cisc105/04F/labs/lab04 .
cd lab04

This will first change your current working directory to your cisc105 subdirectory, then copy the entire lab04 subdirectory from my account, along with all its contents, into the current directory. You'll then have to cd into lab04 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!)

Part 1: Finding the minimum and maximum of a data file

A common problem, when faced with a data file, is to find the highest and lowest values in the data.

For the first part of this lab, you will have a data file integers.dat containing an unknown number of integers. The file ends with the value -1, which is a "sentinel value" (not a part of the actual data, but just a number that marks the end of the data.) You will read the integers from the file one at a time, and compare them against high and low values that you have already read. As you read each new number, adjust your high and low variables accordingly.

Remember that you cannot compare a variable against another unless they both have been assigned values. Given that restriction, how will you start your high and low variables off? One common, but poor, solution is to give the variables arbitrary values that seem very high or low. This is problematic because it limits what data the program will work for. You must use a better way of initializing your variables.

Your program must read data from the file until it reads the value -1, which will be the last number in the file. This kind of special value is called a sentinel value. It is important to choose a sentinel which cannot appear in your data set (do you see why?). Since our data are restricted to positive numbers, we can use -1 as a sentinel value.

Your program will print out the total number of integers read before the sentinel, the highest integer, and the lowest integer.

Can you solve this problem by yourself? If so, write down your ideas before you look at min.c. Is your solution the same? If not, look at how they differ. Do they perform the same task? Initialize the same way? Have the same efficiency in time and memory use?

Suggested approach:
  1. Look at the file min.c, then put it away. If you need to peek at it later, go ahead, but you need to internalize the ideas in this program.
  2. Write an algorithm on paper and decide what belongs in the loop, and what does not.
  3. Next, think about the variables you will need to implement the algorithm; expand your description of the algorithm if necessary. Remember that you do not know how many numbers will be in the file, so the number of variables cannot change based on the data.
  4. Now code only the part of the loop that reads in the data; have it print out each number as it is read.
  5. Add code to count the numbers in the file.
  6. Add code to keep track of the max and min values.
  7. Finally, once everything is working, change your code to print out only the results: min, max and count. Print out a different message if the program contains only the sentinel value (see min.c for an example). Remove the lines that print out each value as it is read, and any other printing that happens inside the loop.
  8. Finish your program and check your style points: output, comments, naming, readability, etc.

Part 2: lab04b.c Altitude, Northernmost, Easternmost, etc.

Now that you have a general idea of the method, you can expand the same concepts to a new problem domain. waypoints.dat is a data file containing information about geographic locations and their altitudes. Your job is to identify five attributes of the data:

  1. the highest elevation
  2. the northernmost location
  3. the westernmost location
  4. the easternmost location
  5. the southernmost location
The format of the file will be in three columns: altitude (in feet), easting, and northing. The data are from www.backpacker.com/destinations/highpoints

The location coordinates are in a reference system that is slowly replacing the use of latitude and longitude, called UTM (Universal Transverse Mercator). The easting number tells where something is ``side-to-side'' on the globe (like longitude); the northing number tells where it falls between the North Pole and the Equator.

You can tell where two points fall relative to one another by comparing the numbers. A larger easting number is further east than a lower one. Likewise a higher northing implies that a point is further North.

Your output should look like this:

> ./lab04b
Westernmost point has easting 130581451
Easternmost point has easting 190505959
Southernmost point has northing 4057300
Northernmost point has northing 5307802
Highest point has altitude 7242
Count is 25
> 

Finishing and Submitting

Script file lab04a.txt should contain a listing, a compile, and a run of your lab04a.c program. Also cat the file integers.dat before you run the program.

Script file lab04b.txt should contain the same elements as lab04a.txt but for lab04b.c (of course, cat the waypoints.dat files instead of the integers.dat file).

Submit lab04a.c, lab04b.c, lab04a.txt and lab04b.txt to WebCT. Print your script files and submit to your TA on paper.

Grading:

lab04a.txt:

 

lab04b.txt:

Total 100pts