bilel kalamoun
bilel kalamoun

Reputation: 5

What is that the Error of Illegal assignment and how to correct it?

procedure tri_selection(t: tab; n: Integer);
var
 i, j, min, aux: Integer;
begin

  for i := 1 to n - 1 do
  begin

    min := i;

    for j := i + 1 to n do
      if t[j] < t[min] then
        j := min;

    if min <> i then
    begin
      aux := t[i];
      t[i] := t[min];
      t[min] := aux;
    end;

  end;

end;

That's supposed to be a correct and well-known code to arrange integers from inferior to superior but compiler still insists saying "illegal assignment to for loop 'j' variable".

What's the problem?

Upvotes: 0

Views: 1815

Answers (2)

Adem_Bc
Adem_Bc

Reputation: 31

you forgot var before t in the header of the procedure

Upvotes: -1

Andreas Rejbrand
Andreas Rejbrand

Reputation: 108963

The problem is here:

for j := i + 1 to n do
  if t[j] < t[min] then
    j := min;                      // <-- Not allowed to assign to FOR loop variable j

You are not allowed to assign to the for loop variable.

Perhaps you meant to write

for j := i + 1 to n do
  if t[j] < t[min] then
    min := j;

Upvotes: 1

Related Questions