I want to test a number of matrices by a procedure. Each matrix should be passsed as a matrix, something like:
type TMatrix = array of array of integer;
procedure test_kernel (mat: TMatrix);
....
test_kernel ([[1, 2], [1, 3]]); // <== does not compile
I cannot find the right syntax to do this correctly (tried with round brackets as well). Does anyone know how to pass a matrix of constant values as a procedure argument? Is it possible at all?
EDIT
As what I want is not possible I decided to use:
type TMatrix = array of integer;
procedure test_kernel (rows, cols: integer; mat: TMatrix);
....
test_kernel (2, 2, [1, 2,
1, 3]);
So I get the illusion and readability of matrices. Thank you all!
Answer 1: In C++, the declaration of an argument to a function can take place as const.
To pass an array as a parameter to a function, pass it as a pointer (since it is a pointer). For example, the following procedure sets the first n cells of array A to 0. Now to use that procedure: int B[100]; zero(B, 100);
The call by value method of passing arguments to a subprogram copies the actual value of an argument into the formal parameter of the subprogram. In this case, changes made to the parameter inside the function have no effect on the argument.
Arguments are Passed by Value The parameters, in a function call, are the function's arguments. JavaScript arguments are passed by value: The function only gets to know the values, not the argument's locations. If a function changes an argument's value, it does not change the parameter's original value.
There actually is a possiblity to do this if you use a slightly different declaration of TMatrix, but IMHO it doesn't increase the readability of the code:
type
TVector = array of integer;
TMatrix = array of TVector;
procedure test_kernel (mat: TMatrix);
....
test_kernel(TMatrix.Create(TVector.Create(1, 2), TVector.Create(1, 3)));
You cannot do what you wish with constants or open arrays. TMatrix
is a dynamic array and you cannot have constants that are dynamic arrays. And a matrix is 2D but open arrays can't be nested. You cannot have an open array of open arrays. If this was just a vector, i.e. 1 dimensional, then you could indeed use open arrays. However, since you have a 2D matrix, open arrays can't help.
You will need to use a variable that is initialised at runtime. You can do this easily enough in an initialization
section if you truly have a constant.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With