Back to DFS's Pascal Page


Analyzing a Large Text III

Sorting Records

Your previous program introduced you to text analysis. A major problem with the design of the program was that the relationship between the words and their frequencies of occurrence was not made explicit. You, as the programmer, had to maintain the relationship between a word and its frequency by purposefully moving/copying one and the other simultaneously.

Programming languages usually provide a method for maintaining the relationships among various pieces of related information. Pascal uses the programmer-defined data type called a record. Before attempting this programming task, you should study the Records page.

In order to pass an array of records to a procedure, it is necessary to declare a special type for the array. This is similar to what needs to be done to pass any other array to a procedure; it just requires that the type for the record needs to specified as well. For the basics about establishing a type for an array, reread the One-Dimensional Arrays II page.

For the current task, you should use the following type section:

type
   SingleWordInfo = RECORD
     orig_pos : integer;
     word : string[15];
     freq : integer
   end;
   ArrayRecType = array [LO..MAX] of singlewordinfo;

The record definition includes a field for the order of first occurrence for the word, which will allow the data to be resorted into the original order.

The var should then include:

   words : ArrayRecType;   {Array of all unique word records}

Temporary storage of an array element (a whole record) inside a procedure can be accomplished using the declaration and assignments similar to those below:

var
   tempword : singlewordinfo;

       tempword := words[large];
       words[large] := words[current];
       words[current] := tempword;

The overall structure of the program should be the same as that in the previous program. The only differences are the following:

  1. You must use records instead of parallel arrays.
  2. You must pass the array of records as a parameter to the procedures which will use it.
  3. You must process the user's choice of display for the data in a loop so that all three display possibilities given on the menu can be shown during a single run of the program.

Consequently, the following will be the structure of your program.

  main
      |Initialize
      |Introduction
      |GetFilename
      |ProcessFile
                  |f WordAlready : integer
      |f LargestFreq : integer
      |f BarRatio : real
      |PrintStats
      |Menu
           |PrintOrigOrder
                          |SortOrigOrder
                          |PrintTable
           |PrintAlphaOrder
                          |SortAlpha
                          |PrintTable
           |PrintNumOrder
                          |SortNumeric
                          |PrintTable

© DFStermole 2002
Created 15 Mar 02