TIP: Click on subject to list as thread! ANSI
echo: z3_pascal
to: All
from: Peter Lavender
date: 1996-04-08 14:36:46
subject: Sorting an array?

Hi All,

I have 3 arrays one which holds names, one holds flight number, and the 3rd
holds seat numbers.

I have written a procedure that has a menu that allows you to choose which
array to sort on, Name, Flight, Seat. it then calls another procedure that
does the sorting.

This is where I'm having the problem, I'm using the bubble sort method as
described in one of my text books, but the sort is failing.  I have spent
the best part of 6 hours stepping over the procudre but can't for the life
of me see where it is failing.

I'll grab the procedure and explain at the end.


*-=-=-=-=[ Begin: aline15.pas ]=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

{------------------------Sort---------------------------------------------}


procedure Sort (SortOn: string);

var

  TempName, TempFlight, WhichArray: string;
  Cnt, i, TempSeat, Pass: integer;
  NoSwitches: boolean;


begin
  if SortOn = 'Cust' then
    WhichArray:= 'Customer';
  if SortOn = 'FltNumber' then
    WhichArray:= 'FltNum';
  if SortOn = 'CustSeat' then
    WhichArray:= 'Seat';

  Pass:=0;
{  repeat
    Pass:= Pass+1;
    Noswitches:= true;}
  for Cnt:= 1 to TotalPointer do
    begin
      for i:= 1 to TotalPointer do
        begin
        if WhichArray[i] > WhichArray[i+1] then
          begin
            NoSwitches:= false;

            TempName  := Customer[i];
            TempFlight:= FltNum[i];
            TempSeat  := Seat[i];

            Customer[i]:= Customer[i+1];
            FltNum[i]  := FltNum[i+1];
            Seat[i]    := Seat[i+1];

            Customer[i+1]:= TempName;
            FltNum[i+1]  := TempFlight;
            Seat[i+1]    := TempSeat;
          end; {if}
        end;{for i inner loop}
    end;{for Cnt outer loop}
{  until NoSwitches;               }
end; {procedure}

{-------------------------------------------------------------------------}

{------------------------SortMenu-----------------------------------------}

procedure SortMenu;

 {This procedure allows the choice of sorting by name, flight or seat, a
  seperate procedure called Sort is used for the actual sorting.}
const

  Quit = 4; { Set the number to the matching option in the menu to quit}


var

  Col, Row, Errorcode, Choice, LastRow: integer;
  CheckChoice, Enter, ArrayType: string;

begin
  Choice:= 1;
  while Choice  Quit do
    begin
      LastRow:=0; {This allows for checking on display procedure}
      clrscr;
      Col:= 2;
      Row:= 3;
      gotoxy(Col,Row);
      Write ('1)   Sort by Name.');
      Row:=Row + 2;
      gotoxy(Col,Row);
      Write ('2)   Sort by Flight.');
      Row:=Row + 2;
      gotoxy(Col,Row);
      Write ('3)   Sort by Seat.');
      Row:= Row + 2;
      gotoxy(Col,Row);
      Write ('4)   Quit back to Main Menu.');
      Row:= Row + 3;
      gotoxy(Col,Row);
      Write ('Choose how you wish to sort: ');
      ReadLn(CheckChoice);
      val(CheckChoice,Choice,ErrorCode);
        if ErrorCode = 1 then
          begin
            Row:= Row + 3;
            gotoxy(Col,Row);
            Write ('You must make a choice from the menu.');
          end;{if error.}

      case Choice of

        1 :   begin
                ArrayType:= 'Cust';
                Sort (ArrayType);
                DisplayAll(LastRow);
              end;

        2 :   begin
                ArrayType:= 'FltNumber';
                Sort(ArrayType);
                DisplayAll(LastRow);
              end;

        3 :   begin
                ArrayType:= 'CustSeat';
                Sort(ArrayType);
                DisplayAll(LastRow);
              end;

        4 : {Quit here}
      else
        begin
          Row:= Row+2;
          gotoxy(Col,Row);
          WriteLn ('This is not a valid Choice: ',CheckChoice,'.');
          Row:= Row + 2;
          gotoxy(Col,Row);
          Write ('Hit Enter to continue. ');
          ReadLn (Enter);
        end;

      end; {case choice}
    if LastRow > 1 then {Since displayall only runs through a counter to display
                         the contents of the array, we need something to stop
                         further execution of the program, this is done here in
                         the calling procedure.}
      begin
        Row:= LastRow;
        Row:= Row + 2;
        Col:= 2;
        gotoxy(Col,Row);
        Write ('Hit enter to continue.');
        ReadLn(Enter);
      end; {if lastrow check}
   end; {while}
end; {procedure}


=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=[ End: aline15.pas ]=-=-=-=-*

Right.. the idea is that the user will choose sort on Name, the choice is
sent out as a parameter, where the called procedure will check, and then
assign the right array to "WhichArray".

Anyone willing enough to take a look and see why this sort is failing?

When stepping through, I noticed that element 2 and 3 were changing the the
rest weren't moving much..

Cya,

Pete

... Foolish fears drive away good fortune.
--- GoldED/386 2.50 UNREG
--- 
* Origin: Reality Sucks Back from Reality. (3:635/309)
SEEN-BY: 633/267 270
@PATH: 635/309 727 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.