Exercise #1: Review of Pascal

The file /u2/stone/datasets/Iowa-counties.dat contains the name, population, and area (in square miles) of each county in Iowa. It is a text file containing one line for each county, in alphabetical order. Each line contains exactly twenty-eight characters (followed by a newline). Columns 1 through 13 contain the county name, left-justified; columns 14 through 16 are spaces; columns 17 through 22 contain the population, right-justified; columns 23 through 25 are spaces; and columns 26 through 28 contain the area in square miles, right-justified. Here's a sample line:

Poweshiek        19018   586
The population density of a region is the number of inhabitants of that region divided by its area. For example, if the statistics just exhibited indicate that the population density of Poweshiek county is 19018/586, or 32.45, inhabitants per square mile.

Design, write, and test a Pascal program that reads all of its input from the file named above and writes out a new file in the user's current working directory. This file should be named Iowa-counties-by-density.dat. In it, the counties should be listed in descending order by population density. There should be one line for each county, comprising exactly twenty-six characters and then a newline. Columns 1 and 2 should contain the county's rank in descending order by population density; column 3 should contain a period and column 4 a space; columns 5 through 17 should contain the name of the county, left-justified; columns 18 and 19 should be blank; columns 20 through 26 should contain the county's population density, in inhabitants per square mile, rounded to the nearest hundredth, right-justified. Here's a sample line from the desired output file:

36. Poweshiek        32.45
This exercise will be due on Friday, September 6.


Here's how to submit your program by electronic mail. Suppose that your Pascal program is in counties.p, and the executable version is in counties. Issue the following sequence of commands in a terminal window:

submit counties.log
cat counties.p
./counties
cat Iowa-counties-by-density.dat
When all of the output from the last command has been displayed, press <Control/D>. This will create a file named counties.log in your current working directory, containing the complete text of your Pascal file, a transcript of the run of your program, and a listing of its output. Mail this log file to me by issuing the command
elm stone <counties.log
Once you have sent in the log file, you can delete it.


created August 25, 1996
last revised August 26, 1996