Hi!
i am having trouble sorting an array using a for loop.
the problem occurs when using iterations i and i+1

for example
count:=0
for i:=0 to count do begin
if a > a[i+1] then begin
....

end
count+1
end

problem is when the for loops reaches the LAST array value i , i+1 does not exist hence the problem.
I have tried using
" for i:=0 to count-1 do begin"
this does not help.

any suggestions?

Hi!
i am having trouble sorting an array using a for loop.
the problem occurs when using iterations i and i+1

for example
count:=0
for i:=0 to count do begin
if a > a[i+1] then begin
....

end
count+1
end

?

this example absolutly worng …

## All 3 Replies

I think that you do understand the for loop first,and then the shoriting algorithm.
I made a program that shorts an array

``````program forloop;

uses crt;

var a:array[1..10]of byte;
temp:byte;
i,k:byte;

begin
clrscr;

(*fill the array with random numbers*)
randomize;
for i:=1 to 10 do begin
a[i]:=random(100)+1;
end;

(*write the result to the screen*)
writeln('first run: ');
for i:=1 to 10 do begin
write(a[i],' , ');
end;
writeln;

(*short the numbers of the array a*)
for i:=1 to 9 do begin
for k:=i+1 to 10 do begin
if a[i] > a[k] then begin
temp:=a[i];
a[i]:=a[k];
a[k]:=temp;
end;
end;
end;
writeln;
writeln;
writeln('shorted results: ');
(*write the result to the screen*)
for i:=1 to 10 do begin
write(a[i],',');
end;
writeln;

(*press any key to quit*)
while not keypressed do continue;
end.
(*created by FlamingClaw 2010.02.04.*)``````

the basic syntax of the for loop is:

FOR <VAR>:=<MIN_VALUE> TO <MAX_VALUE> DO BEGIN
<COMMANDS HERE>
END;

OR ANOTHER VARIATION IS:

FOR <VAR>:=<MAX_VALUE> DOWNTO <MIN_VALUE> DO BEGIN
<COMMANDS HERE>
END;

Hi!
i am having trouble sorting an array using a for loop.
the problem occurs when using iterations i and i+1

for example
count:=0
for i:=0 to count do begin
if a > a[i+1] then begin
....

end
count+1
end

?

this example absolutly worng :'(
don't hate me about it,know the basic syntax first.
listen my code!

HI!
FlamingClaw
I checked my syntax. that is where the problem was.