TIP: Click on subject to list as thread! ANSI
echo: z3_pascal
to: Peter Lavender
from: Darryl Luff
date: 1996-04-16 01:03:00
subject: Sorting an array?

> 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.


    Hi Peter,

 > 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;

    If WhichArray contains 'Customer', then WhichArray[1] = 'C',
WhichArray[2] = 'u', etc. You're sorting using the string in WhichArray
instead of the data in the arrays.

    To do this, you'd have to use something like:

VAR
  fltNum    : Array[1..10] OF String;
  customer  : Array[1..10] OF String;
  sear      : Array[1..10] OF Integer;

PROCEDURE Sort(sortOn : Char);
BEGIN
  IF (sortOn = 'C') THEN
  BEGIN
    { do the sort using customer    }
  END
  ELSE IF (sortOn = 'F') THEN
  BEGIN
    { do the sort using fltnum }
  END
  ELSE IF (sortOn = 'S') THEN
  BEGIN
    { do the sort using seat }
  END;
END;

You can't use a string as the name of an array, ie:

VAR
  test : Array[1..10] OF String;
  s    : String;
BEGIN
  s := 'test';
  Write(s[1]);  { this will always write 't', whatever's in the array }
  Write(s[2]);  { and this will always write 'e' }
END;

cya, Darryl.

--- FMail 0.96â
* Origin: Point Blank - Hoppers Crossing Vic (3:632/506.4)
SEEN-BY: 633/267 270
@PATH: 632/506 998 635/503 50/99 635/544 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™.