Domanda

I've been trying to run the following program, but the compiler keeps telling me that identifier n is not working. What the program should do is to do as many times as n's value, the procedure file. n can't be a constant, because is read at runtime. Can anyone please help me?

Program num;

Var
f:Text;
b,g:array [1..n] of integer;
m,n:Integer;
c:String[1];

Procedure thenum (a:array [1..n] of integer);
Begin
for i:= 1 to n do
repeat
Read (f,a[i]);
Write(a[i]);
Until(a[i]=' ');
End;
End;

Procedure sth ( j:array [1..n] of integer);
begin
for k:= 1 to n do
while not seekEoln and eof(f) do
begin
read(f,j[k]);
Write(j[k]);
end;
End;
End;

procedure space;
begin

Read(f,c);
Write(c);
end;

procedure file
begin
Assign(f,'textfile.txt');
Reset(f);
thenum (b[a]);
space;
sth (g[k]);
Close(f);
Readln;
End;


Begin
Assign(f,'textfile.txt');
Reset(f);
repeat
Read (f,n);
Write(n);
Until(n=' ');
End;
Read(f,c);
Write(c);
while not seekEoln and eof(f) do
begin
read(f,m);
Write(m);
end;
file;
End.
È stato utile?

Soluzione

Pascal doesn't support variable-sized arrays declared as fixed-length.

Most modern compilers (Delphi/FreePascal) support dynamic arrays, where the length is set at runtime. The lower bound is always zero, and the upper bound is length - 1:

var
  Arr: array of Integer;
  L, H, Len: Integer;
  ArraySize: Integer;
begin
  Len := 10;                // Value of Len can be input at runtime
  SetLength(Arr, Len);
  L := Low(Arr);            // Will be 0
  H := High(Arr);           // Will be Len - 1, because first index is 0
  ArrSize := Length(Arr);   // Will be Len
  WriteLn('Low: ', L, ' High: ', H, ' Length: ', ArrSize);  
  ReadLn;
end;

Here's a complete sample (in Delphi, but it should work with FreePascal) that accepts input from the console and uses it to set the length, and show you the low, high, and length values like above. It also demonstrates how to pass the array to a procedure that has no knowledge of the number of elements in the array.

program Project1;

uses
  SysUtils;

// Lists the items in an array to the console without
// knowing in advance the size of the array
procedure ListAnArray(const TheArray: array of Integer);
var
  x: Integer;
begin
  // Retrieve the valid low and high indexes, and
  // loop through the array to output each value
  for x := Low(TheArray) to High(TheArray) do
    WriteLn('TheArray ', x, ' value: ', TheArray[x]);
end;

// The variables we'll be using (a few more than I would 
// normally use, just to make the example clearer)
var
  ArraySize: Integer;            // Length of array input by user
  lowIdx, highIdx: Integer;      // low array index, high index
  ArrayLen: Integer;             // Length of array (set at runtime)
  Counter: Integer;              // Loop counter
  TestArray: array of Integer;   // Our dynamic array

begin
  Write('Enter array length: ');      // Prompt user
  Readln(ArraySize);                  // Read input

  Writeln('You entered: ', ArraySize);
  WriteLn('');                        // Blank line separator

  SetLength(TestArray, ArraySize);    // Size the array
  lowIdx := Low(TestArray);           // Get low index (always 0)
  highIdx := High(TestArray);         // # of elements - 1
  ArrayLen := Length(TestArray);      // Same as user entered
  Writeln('Low: ', lowIdx,            // Output what we've learned
          ' High: ', highIdx,
          ' Length: ', ArrayLen);
  WriteLn('');                        // Blank line separator

  for Counter := lowIdx to highIdx do // Fill the array with values
    TestArray[Counter] := Counter * Counter;

  ListAnArray(TestArray);             // Pass array to procedure
  WriteLn('');                        // Blank line separator

  WriteLn('Press Enter to quit...');  // Pause to allow reading.
  Readln;
end.

Altri suggerimenti

The compiler does not know the value of n at compile-time. Normally you would define some appropriate maximum size as a constant, e.g. N_MAX, and then use that for your array dimension. At run-time you would make sure that n <= N_MAX.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top