Search code examples
delphidelphi-7

Custom procedure for sorting an array


Im going through one of my old exam papers, preparing for my finals, and for the love of life, I cant figure out how to do this! The program was working earlier, but it wasn't sorting the array. Now Im getting an error saying EAccess violation with message: access violation at address 00404BDE

Here's my code (its kind of long, maybe you can help me spot my error) :

private

    { Private declarations }
    iCount : Integer;
    arrDams : array [1..200] of string;
    Procedure List;
    procedure Display;
    procedure Sort;
    procedure Search (sDam : String);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Display;  //Display with Numbers
var
  k : Integer;
begin
  for K := 1 to 200 do
    begin
      RedOut.Lines.Add (IntToStr(k) + '.) ' + (arrDams[k]));
    end;
end;

procedure TForm1.FormCreate(Sender: TObject); // Create
begin
//
end;

procedure TForm1.List; // TextFile to array
var
  MyFile : TextFile;
  k : Integer;
begin
  If FileExists('Dams.txt') <> True
    then Application.Terminate;

  AssignFile (MyFile, 'Dams.txt');
  Reset(MyFile);
  For K := 1 to 200 do
    begin
      Readln(MyFile, arrDams[k])
    end;
end;

procedure TForm1.Search(sDam: String); // Search
begin

end;

procedure TForm1.Sort; // Sort;
var
  K,L : byte;
  sKeep : string;
begin
  for k := 1 to iCount -1 do
    begin
      for l := k + 1 to iCount do
        begin
          if arrDams[k] > arrDams[L] then
            begin
              sKeep := arrDams[k];
              arrDams[k] := arrDams[L];
              arrDams[L] := sKeep
            end;
        end;
    end;
end;

procedure TForm1.btnListClick(Sender: TObject);
begin
 List;
 Display;
end;

procedure TForm1.btnDisplayClick(Sender: TObject);
begin
  display;
  sort;
end; //<---------- ERROR OVER HERE!

end.

Theres 3 buttons at the top of the form, namely Show list, Make new textfile with list and Sort list alphabetically. The button Im working on is to sort the list. This question paper says I must make a sort procedure and must be called when the Sort Button is clicked.

Thanks for any advice/help

P.S.

Can you please point me to a link where they explain Selection Sorting in depth - the logic isn't with me on this..


Solution

  • You don't initialize iCount. So it is 0. Therefore iCount-1 is -1. However, you use Byte, an unsigned type, for your loop variable. Now, -1 when interpreted as an unsigned Byte is 255. If you follow this all through it means that you access the array out of bounds. In fact what happens is that the inner loop executes exactly once with a value of l equal to 0 and k equal to 255.

    Were you to enable the range checking compiler option, you would have encountered a runtime error as soon as you run off the end of the array.

    Presumably you want to initialize iCount to some value. I cannot tell what, but you will know.

    Beyond that, stop using unsigned types for loop variables. Replace Byte with Integer.