Return to Mr Belvedere's Programming Nook


Records Example
© 1996 by Bobby Griggs.


(**************************************************************************)
(*
   Robert C. Griggs, Jr.
   User C181A09
   Homework Assignment
   Program name - NAMERECS.P
   September 5, 1991
                                                                          *)
(**************************************************************************)
(*
   This program is designed to accept input of first name, last name, and
   identification number for ten class members.  The user may enter all ten,
   or may discontinue entry of data at his choice.  Once the user has
   entered all the names he desires, the program sorts the last names in
   ascending order, and then lists the records to the screen.  After
   viewing the display, the user is prompted to hit enter to continue.
   Three separate procedures were used in the program to perform  the
   requirements of the homework assignment.  The first procedure loads
   the records into an array.  The second procedure sorts the last names.
   The third procedure displays the records on the screen.  Control is then
   passed back to the second procedure which then sorts the records by
   identification number in ascending order.  Then the records are once
   again displayed on the screen.
                                                                          *)
(**************************************************************************)
(*
   Algorithm:

   1.  Specify three constants to hold data for minimum and maxium elements
       of the arrays.
            MAXLENGTH for maxium length of a last name or first name
            CLASSSIZE for maxiumum number of persons to be entered
            MIN for minimum array element

   2.  Use a record structure to hold class member's names.
            thename - record consisting of last name, first name, and id
                      firstname and lastname are a packed array 
            classlist - array of thename

   3.  Specify variables for use within program.
            class of type classlist for referencing class records
            records as counter to track number of records entered
            torf as a boolean for use in display procedure to determine if
                 writeln of 'Hit enter to continue' should execute, and in
                 sort procedure to determine whether to sort by last name or
                 identification number

   4.  Run procedure to clear screen.

   5.  Run procedure to enter individual recoreds.
            A.  use the following variables:
                counter for use in intializing arrays for first and last name
                classcnt as counter for number of records entered
                charcount as counter to track characters in name fields
                choice for prompt of whether to enter another record
            B.  prompt user to enter first name
            C.  prompt user to enter last name
            D.  prompt user to enter identification number
            E.  prompt user for whether to enter another record

   6.  Run sort procedure, and sort by last name.
            A.  use the following variables:
                holdperson to hold person's record for sort purposes
                count as counter to be used in sort
                counter as other counter to be used in sort
            B.  execute bubble sort to sort by last name in ascending order
            C.  set boolean torf to false, so next sort will be done by id

   7.  Run procedure to display records to the screen.
            A.  use the following variables:
                counter for use in for loop to list records to screen
                continue for prompt to hit enter to continue
            B.  execute display to the screen

   8.  Run sort procedure again, and sort by identification number.
            A.  use the following variables:
                holdperson to hold person's record for sort purposes
                count as counter to be used in sort
                counter as other counter to be used in sort
            B.  execute bubble sort to sort by id in ascending order

   9.  The main of the program will be executed in the following manner:
            A.  run procedure blankscreen to clear screen
            B.  run procedure entername to input records
            C.  run procedure sortrecs to sort last names in ascending order
            D.  run procedure shownames to display records on screen
            E.  run procedure sortrecs to sort ids in ascending order
            F.  run procedure shownames to display records on screen
                                                                          *)
(**************************************************************************)
(*
   Constant dictionary:

        MAXLENGTH=20 for maxium length of first name and last name
        CLASSSIZE=10 for maxiumum number of class members to be entered
        MIN=1 for minimum array element   
                                                                          *)
(**************************************************************************)
(*
   Type dictionary:

        string20=packed array [1..20] of char - array for name fields
        thename=record - record structure consisting of first name, last
                         name, and identification number
        classlist=array[MIN..CLASSSIZE] of thename - array to hold records
                                                                          *)
(**************************************************************************)
(*
   Variable dictionary:

        class:classlist - class records
        records:integer - counter to track number of names entered
        torf:boolean - for use in display procedure to determine if writeln
                       of 'Hit enter to continue' should execute, and in sort
                       procedure to determine whether to sort by last name
                       or identification number
                                                                          *)
(**************************************************************************)

program namerecs (input,output);

const
     CLASSSIZE=10;  {number of persons in class}
     MIN=1;  {minimum number of array elements}
     MAXLENGTH=20;  {maximum number of characters in name fields}

type
     string20=packed array [1..20] of char;  {array definition for name fields}
     thename=record
                   first:string20;   {first name}
                   last:string20;    {last name}
                   id:integer;       {identification number}
             end;

     classlist=array[MIN..CLASSSIZE] of thename;  {array for records}

var
     class:classlist;  {class records}

var
     records:integer;  {counter to track number of names entered}

var
     torf:boolean;     {for use in display procedure to determine if
                        writeln of Hit enter to continue should execute
                        and in sort procedure to determine whether to
                        sort by last name or identification number}

(**************************************************************************)

(* Procedure to blank screen                                              *)

procedure blankscreen;

     var
          blank:integer;  {counter for blanking the screen}

     begin  {blankscreen}

          for blank:=1 to 25 do
               writeln;

     end;   {blankscreen}

(**************************************************************************)

(* Procedure for entry of records                                         *)

procedure entername (var records:integer);

     var
          counter,            {counter for use in intializing array}
          classcnt,           {counter for number of names entered}
          charcount:integer;  {counter to track characters in name}

     var
          choice:char;        {Y or N for entering another name}

     begin  {entername}

          choice:='Y';
          classcnt:=1;
          blankscreen;
          while ((choice = 'Y') or (choice = 'y')) and (classcnt <= CLASSSIZE) do
               begin  {while statement}
                    for counter:= MIN to MAXLENGTH do
                         begin  {for loop}
                              class[classcnt].first[counter]:=' ';
                              class[classcnt].last[counter]:=' ';
                         end;   {for loop}
                    charcount:=1;
                    write ('Enter first name:  ');
                    while (not eoln) and (charcount <= MAXLENGTH) do
                         begin  {while statement}
                         read (class[classcnt].first[charcount]);
                         charcount:=charcount + 1;
                         end;   {while statement}
                    readln;
                    charcount:=1;
                    write ('Enter last name:  ');
                    while (not eoln) and (charcount <= MAXLENGTH) do
                         begin  {while statement}
                         read (class[classcnt].last[charcount]);
                         charcount:=charcount + 1;
                         end;   {while statement}
                    readln;
                    write ('Enter identification number:  ');
                    readln (class[classcnt].id);
                    classcnt:=classcnt + 1;
                    writeln;
                    if (classcnt <= CLASSSIZE) then
                         begin  {if statement}
                              write ('Enter another name (Y/N)...  ');
                              readln (choice);
                         end;   {if statement}
                    writeln;
               end;   {while statement}
          records:=classcnt - 1;
     end;   {entername}

(**************************************************************************)

(* Procedure to sort records                                              *)

procedure sortrecs (records:integer;torf:boolean);

var
     holdperson:thename;  {hold person's record for sort purposes}

var
     count,            {counter to be used in sort}
     counter:integer;  {counter to be used in sort}

     begin  {sortrecs}

          counter:=1;
          while (counter <= records - 1) do
               begin  {while statement}
                    for count:=1 to (records - counter) do
                         if (torf=true) and ((class[count].last > class[count + 1].last)) then
                              begin  {if statement}
                                   holdperson:=class[count];
                                   class[count]:=class[count + 1];
                                   class[count + 1]:=holdperson;
                              end    {if statement}
                         else
                              if (torf=false) and ((class[count].id > class[count + 1].id)) then
                                   begin  {if statement}
                                        holdperson:=class[count];
                                        class[count]:=class[count + 1];
                                        class[count + 1]:=holdperson;
                              end;   {if statement}
                    counter:=counter + 1;
               end;   {while statement}

     end;   {sortrecs}

(**************************************************************************)

(* Procedure to list records                                              *)

procedure shownames (records:integer;var cont:boolean);

     var
          counter:integer;    {counter for use in for loop}

     var
          continue:char;      {hit enter to continue}

     begin  {shownames}

          blankscreen;
          writeln ('Last name                First name               ID');
          writeln;
          for counter:=1 to records do
               begin  {for loop}
                    write (class[counter].last);
                    write ('     ',class[counter].first);
                    writeln ('     ',class[counter].id);
               end;   {for loop}
          writeln;
          if (cont = true) then
               begin  {if statement}
                    write ('Hit enter to continue ...');
                    read (continue);
                    cont:=false;
               end;   {if statement}

     end;   {shownames}

(**************************************************************************)

    begin  {main}

          entername (records);
          torf:=true;
          sortrecs (records,torf);
          shownames (records,torf);
          sortrecs (records,torf);
          shownames (records,torf);

    end.  {main}