0

My program stops to read anymore lines and ends the program after this procedure like its 'end.' after it (but its not) :

  Procedure BubbleSort;
  var i, j : integer;
  begin
    for i := 0 to count - 1 do begin
      for j := count - 1 downto i do
        if (together[j] > together[j - 1]) then
          Swap(together[j - 1], together[j]);
    end;
  end;
J...
  • 30,968
  • 6
  • 66
  • 143
Filip Sulik
  • 99
  • 1
  • 1
  • 8

1 Answers1

5

I guess the problem is the out of bounds array access. You access index -1. Avoid this by changing the outer loop to:

for i := 1 to count - 1 do begin

I suggest that you enable range checking so that you can learn about out of bounds array access by way of informative runtime errors.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • it worked thanks a lot....but i dont really understand what you wrote up there...could you explain to me a little more ? – Filip Sulik Apr 12 '15 at 21:32
  • When i is 0, the end of the j loop is 0. And so you access index j-1. Which is, well you can do the math. – David Heffernan Apr 12 '15 at 21:34
  • Oops - typed too slow...Seems I can edit my comments but can't delete them. If i is 0 at the beginning of the outer loop, then j will be set to i-1 = -1 at the end of the inner loop. what will happen when you try to access together[-1] ? – Penguino Apr 12 '15 at 21:40
  • @Filip And you set *range checking* and *overflow checking* on in your project options/compiler (exact menu items vary with Delphi version). They should always be on so that you get the actual errors when and where they occur (instead of dependant errors later on). Not many programs need or must have these options off. – Jan Doggen Apr 13 '15 at 10:32
  • @JanDoggen I'd go so far as to say that no program should have these options off. I have a few applications that gain important performance from disabling range checking but, when I do it, it is always locally with compiler directives and only around those methods that are in the performance bottleneck. Something like this : http://stackoverflow.com/a/4998442/327083 but always also conditional on `{$IFNDEF DEBUG}` so that errors are caught during any development if things change. – J... Apr 13 '15 at 11:23