Code:
program new;
//From SRL include, credit to SRL DEVS & EvilChicken!
procedure DeleteValueInIntArray(var Arr: TIntegerArray; ValuePosition: Integer);
var
ArrLen, I: Integer;
begin
ArrLen := High(Arr);
for I := ValuePosition to ArrLen - 1 do
Swap(Arr[I], Arr[I + 1]);
SetArrayLength(Arr, ArrLen);
end;
procedure AutoThis;
var
i, ii, randomNum: Integer;
arr, OTP: TIntegerArray;
begin
setLength(arr, 26);
setLength(OTP, 26);
for ii:= 0 to 4 do
begin
for i:= 0 to 25 do
arr[i]:= i+1;
for i:= 0 to 25 do
begin
randomNum:= Random(high(arr));
OTP[i]:= arr[randomNum];
DeleteValueInIntArray(arr, randomNum);
end;
for i:= 0 to 25 do
writeln(OTP[ii]);
writeln(chr(13) + chr(13));
end;
end;
begin
AutoThis;
end.
Error:
Error: Access violation at line 25
I'm pretty rusty, it's been a while since I've programmed like I used to back in the day. So this one's got me. I'm only trying to access index 0-25 and the length is 26, I can't figure this one out. Any help guys? Thanks in advanced.
Baked
EDIT:
[RESOLVED]
The error was due to line 25 resetting my arr length to be one shorter, I just had to drop line 20 down 4 lines into the first outter loop.
Benland100 helped me discover my error:
Code:
[02:23] <@BenLand100> the make-my-array-shorter one
[02:23] <@BenLand100> then you try to set all 26 elements again next loop
[02:23] <@BenLand100> obvs you segfault
[02:24] <@BenLand100> if you move line 20 inside that for loop it'll work
ianh and R0b0t1 was also helpful towards me in the making of this script. I am aware there is a better way to do this, as pointed out be Benland in the irc channel, this was just my original thought so I wanted to make it come alive before abandoning it for educational and memory refreshing purposes. Thanks everyone.