The problem is that this program crashes:

program Arrays;
 
var
   a: array[1..5] of Integer;
   i, j, tmp: Integer;
 
begin
   a[1] := 23;
   a[2] := 45;
   a[3] := 12;
   a[4] := 56;
   a[5] := 34;
   for i := 1 to 4 do
      for j := i to 5
         if a[j] > a[j + 1] then
            begin
               tmp := a[j];
               a[j] := a[j + 1];
               a[j + 1] := tmp;
            end;
end.

I can't figure out what's wrong with it, it's in the tutorial that way exactly. I've been trying to get it right for a while but I just can't make it work. I'm using the latest Free Pascal compiler. The tutorial can be found here.

Thank You.

fixed your problem at IRC, but just for the people who are sitting are the code thinking about it, when it's already been fixed :)

Your doing
a[j + 1] := tmp;
on th third last line.

Meaning when j reaches 5, your doing
a[5+1] which is a[6]

Which does NOT exist, as you only created 5 elements for your array at the very start..

Error coded 201 came out of this as you mentioned, which is an outofbounds error.

Confirmation: http://community.freepascal.org:10000/bboards/message?message_id=144869&forum_id=24092


Enjoy.

are you sure that the way it's implemnented in VB:

a: array[1..5] of Integer;

it looks really weird for me... seroiusly.. :-/

btw, ignore this post I'm just looked at it and it's really weird...
but yeah, above me said it correctly :)

Your code has a fundemental floor as pointed out by pim your code

var
a: array[1..5] of Integer;
   i, j, tmp: Integer;

begin
   a[1] := 23;
   a[2] := 45;
   a[3] := 12;
   a[4] := 56;
   a[5] := 34;
   // old code
   //for i := 1 to 4 do
   // new line of code next
   for i := 5 Downto 1 do
   // this new line counts down thru the array not up thru it
      for j := i to 4 do
         if a[j] > a[j + 1] then
            begin
               tmp := a[j];
               a[j] := a[j + 1];
               a[j + 1] := tmp;
            end;
end;

The above code works. But i have not tested it agaist other array lists. There are severl well written sort routines for delphi / Pascal that you should be able to find on the net

Please try this. (I have defined an array as global variable.)

procedure TForm1.Button1Click(Sender: TObject);

var
i, j, tmp: Integer;

begin
for i := 1 to Limit do
for j := i + 1 to Limit do
if a > a[j] then
begin
tmp := a;
a := a[j];
a[j] := tmp;
end;
end;

Hi,

I see that you try to do a Bubble sort. The correct algorith is :

program Arrays;
 
var
   a: array[1..5] of Integer;
   i, j, tmp: Integer;
 
begin
   a[1] := 23;
   a[2] := 45;
   a[3] := 12;
   a[4] := 56;
   a[5] := 34;
   for i := 1 to 4 do // for i := 1 to n - 1 do
      for j := i + 1 to 5 //for j := i + 1 to n do
         if a[i] > a[j] then 
            begin
               tmp := a[i];
               a[i] := a[j];
               a[j] := tmp;
            end;
end.

Ionut

Edited 6 Years Ago by Ionelul: n/a

This article has been dead for over six months. Start a new discussion instead.