Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass a matrix of constant values as an argument of a procedure call

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!

like image 412
Arnold Avatar asked Dec 04 '11 11:12

Arnold


People also ask

Can a constant be passed as an argument in function?

Answer 1: In C++, the declaration of an argument to a function can take place as const.

How do you pass an array as a parameter?

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);

How parameters are passed to procedures in call by value method?

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.

How can we pass arguments in function calling?

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.


2 Answers

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)));
like image 156
Uwe Raabe Avatar answered Sep 28 '22 03:09

Uwe Raabe


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.

like image 20
David Heffernan Avatar answered Sep 28 '22 01:09

David Heffernan