Reccho Reccho - 3 years ago 350
Pascal Question

Array Range error in Pascal

I'm working on some homework in Pascal for class, and I've hit a snag. My basically works, but our homework submission site wasn't giving me full credit. I emailed my professor and he clued me in to the {$r+} directive. When I include that, I get run-time error 201. I know the issue relates to range-checking, but I can't figure out which changes will fix the problem. Any help is greatly appreciated, thanks.

{$mode Delphi}
program InversePerm;
N: integer; // Size of set of numbers
i: integer; // counter
x: integer; // array-slot counter
perm: array of integer; // THE array

setlength(perm, N); // sets array size to user input
for i := 1 to N do
perm[x] := i;
for i := 1 to N do
write(perm[i], ' ');

Answer Source

I think your issue is on the line:

perm[x] := i;

I think what you meant to write was:

perm[i] := x;

Otherwise if I enter 100 to x then your code does perm[100] = 1 on the first iteration. If 10 was entered for N then that would give you a range error.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download