When I run it,it appear error 88:'')'' is expected in A[L]:=sort;.
I think it is no need to add '')''.
What error 88 mean:?:

const
  max = 100;
var
  A: array[1..max] of real;

function Sort(L, R:integer):real;
begin

for L:= L to R-1 do begin
  if A[L]<A[L+1] then begin
  sort:=A[L+1];
  A[L+1]:=A[L];
  A[L]:=sort;
  end;
end;

end;

var
  I, N, L, R: integer;

{ Main Program }
begin
  readln(N);
  for I := 1 to N do
    read(A[I]);
  readln;
  readln(L, R);
  sort(L, R);

  for I := 1 to N - 1 do
    write(A[I]:0:7, ' ');
  writeln(A[N]:0:7)
end.

Dani AI

Generated

The compiler error saying "')' expected" at the line A[L] := sort; is a syntax complaint: Sort was declared as function Sort(L, R: integer): real; so the compiler expects a call like Sort(L, R) (parentheses with arguments). Writing A[L] := sort; makes the parser look for ( or ) and thus raises that message. Two clear fixes: call the function with its parameters, or turn Sort into a procedure and call it without assigning a return value.

Do not use the function name as a temporary variable. In Pascal a function name holds the return value, but using it to swap array elements is confusing and error-prone. Also avoid reusing parameter names as loop counters (your for L := L to R-1 masks the intent and makes the code hard to read). Use local counters and a Temp: real for swapping.

A simple, safe in-place sort as a procedure (different approach than the examples already posted) — selection-style —:

procedure SortRange(L, R: Integer);
var
  i, j: Integer;
  Temp: Real;
begin
  for i := L to R - 1 do
    for j := i + 1 to R do
      if A[i] < A[j] then
      begin
        Temp := A[i];
        A[i] := A[j];
        A[j] := Temp;
      end;
end;

Call it with SortRange(L, R); (no assignment). Quick practical checks: ensure 1 <= L < R <= N, read inputs in the right order, and compile messages differ by compiler — read the textual part, not only the number.

As suggested, use a local Temp instead of the function name; correctly pointed out the parser was waiting for parentheses; and ’s advice to prefer a procedure for an in-place sort is the usual, practical choice.

Recommended Answers

All 3 Replies

Hi yozuca,

I think the problem is that you try to assign to that real entity(a[L]) the result of the function. "sort" function has two parameters, so the correct assignment would be :

a[L] := sort(FirstParameter, SecondParameter); here you call the function.
I suggest to use a local variable of type real.

function Sort(L, R:integer):real;
var
   Temp : real;
begin

for L:= L to R-1 do 
  if A[L]<A[L+1] then begin
  Temp:=A[L+1];
  A[L+1]:=A[L];
 A[L]:=Temp;
  end;

sort := 1; //another suggestion is to use a procedure instead of a function. A function is usefull when you have to return a value.

end;

Ionut

First , error 88 is that the compiler waits for the (
and second I fixed your program.

program solution01;

const max = 100;

var A: array[1..max] of real;

function Sort(L, R:integer):real;
begin
     for L:= L to R-1 do begin
         if A[L]<A[L+1] then begin
            sort:=A[L+1];
            A[L+1]:=A[L];
            sort:=A[L];   (*A[L]:=sort; wrong reference!Look at mine!*)
         end;
     end;
end; (*of Sort*)

var I, N, L, R: integer;

{ Main Program }
begin
     readln(N);
     for I := 1 to N do read(A[I]);
     readln;
     readln(L, R);
     sort(L, R);
     for I := 1 to N - 1 do write(A[I]:0:7, ' ');
     writeln(A[N]:0:7);
end.
(*fixed by FlamingClaw.2010.04.13.Hungary*)

To Add to what FlamingClaw said
Your function needs to be refined still further. Understand the difference between a function and a procedure. You have not used the result of the function in the application but seem to have tried to use the result in the function itself.

procedure Sort(L, R:integer); //--> use procedure as no result is returned
Var Temp:Real;
       aCount:Integer;
       HasSwapped :Boolean;
begin
    Repeat
       HasSwapped:=False;
       for aCount:= L to R-1 do 
       begin
           if A[aCount]<A[aCount+1] then 
           begin
              Temp:=A[aCount+1];
              A[aCount+1]:=A[aCount]; 
              A[aCount]:=Temp;
              HasSwapped:=True;
           end;
       end;
    Until HasSwapped=False;
(* Search for bubble sort for a more efficient sorting algorithm more suited to array sorting  *)
end; (*of Sort*)

Hope this helps
Regards Cao

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.