1

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.
3
  • Isn't an array index zero based in Pascal? Commented Oct 29, 2017 at 10:44
  • 2
    @Hintham: Not necessarily, but in this case yes. Op would do better with for i := Low(Perm) to High(Perm) ... Commented Oct 29, 2017 at 10:54
  • I switched the loop conditions to your suggestion, thank you. Could it be that one of my variables is too small? I thought including the {$mode Delphi} directive would take care of anything like that... Commented Oct 29, 2017 at 11:02

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Ah thanks a ton. That fixed it, I no longer get the run-time error.
The array uses zero-based indices. You have to mention that in the answer.
No, I don't. That issue was dealt with in the comments on the question itself. The OP had already stated that they had addressed that issue, before I answered. Someone else would have down voted me for copying others work if I had mentioned Pascal's variable base system.
Comments are transient. An answer should stand alone. Had you addressed the entire question I would have upvoted instead of downvoting. The fact that somebody comments on an issue does not mean you are not allowed to cover that in an answer.
Has nothing to do with being pedantic. Without fixing the off-by-one array indexing as well, the code in question will still fail. You should edit your answer to include that issue as well. It probably is worth pointing out that all dynamic arrays in Pascal are zero based. Presuming that something will be seen in a comment is simply a wrong assumption. A future reader looking for a solution might not bother to read the question's comments. Rather than simply trying to boost your rep, try posting an answer that actually solves the problem and shares knowledge for others to use later.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.