Reccho
Reccho

Reputation: 97

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}
{$r+}
program InversePerm;
var
    N: integer; // Size of set of numbers
    i: integer; // counter
    x: integer; // array-slot counter
    perm: array of integer; // THE array

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

Upvotes: 1

Views: 803

Answers (1)

Philip Smith
Philip Smith

Reputation: 2801

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.

Upvotes: 2

Related Questions