I have this function to solve 3x4 matrix by Gaussian elimination. But i got this error when running the program.
[Error] Unit1.pas(79): Identifier expected but 'ARRAY' found
[Error] Unit1.pas(79): Undeclared identifier: 'TArray'
function Solve3x4MatrixByGaussianElimination(A: array of array of Extended): TArray<Extended>;
const
Rows = 3;
Cols = 4;
var
i, j, k: Integer;
factor: Extended;
X: array of Extended;
begin
SetLength(X, Rows);
for i := 0 to Rows - 2 do
begin
for k := i + 1 to Rows - 1 do
begin
if Abs(A[i, i]) < Abs(A[k, i]) then
begin
for j := 0 to Cols - 1 do
begin
factor := A[i, j];
A[i, j] := A[k, j];
A[k, j] := factor;
end;
end;
end;
for k := i + 1 to Rows - 1 do
begin
factor := A[k, i] / A[i, i];
for j := i to Cols - 1 do
A[k, j] := A[k, j] - factor * A[i, j];
end;
end;
X[Rows - 1] := A[Rows - 1, Cols - 1] / A[Rows - 1, Rows - 1];
for i := Rows - 2 downto 0 do
begin
X[i] := A[i, Cols - 1];
for j := i + 1 to Rows - 1 do
X[i] := X[i] - A[i, j] * X[j];
X[i] := X[i] / A[i, i];
end;
Result := X;
end;
I wonder how to solve this, I've been looking to some solution such as the Uses I need to use, but still failed. I am working on Delphi 7.
Generics were introduced in Delphi 2009, they simply didn't exist yet in Delphi 7, which is why you are getting an error on TArray<Extended>
.
If you really want to return a Dynamic Array, you need an explicit type
for it, eg:
type
TArrayOfExtended = array of Extended;
function Solve3x4MatrixByGaussianElimination(A: ...): TArrayOfExtended;
...
var
...
X: TArrayOfExtended;
begin
...
Result := X;
end;
That being said, when you use array of ...
directly in a parameter type, it declares the parameter as an Open Array, not a Dynamic Array. You can't use array of array of ...
as a parameter type because an Open Array of Open Array makes no sense.
In this situation, to pass in a 2D array, you can use either:
a static array:
type
TMatrix = array[0..2] of array[0..3] of Extended;
TArrayOfExtended = array of Extended;
function Solve3x4MatrixByGaussianElimination(var A: TMatrix): TArrayOfExtended;
a Dynamic Array of Dynamic Array:
type
TArrayOfExtended = array of Extended;
T2DArrayOfExtended = array of TArrayOfExtended;
function Solve3x4MatrixByGaussianElimination(A: T2DArrayOfExtended): TArrayOfExtended;