Number of elements in an array
This function yields the number of elements of the 1st and 2nd dimension of an array.
Input
Input variable (array dimension 1,2 or 3) whose number of elements is to be determined
Output
Yields number of elements in the first dimension
Yields number of elements in the second dimension
If input Array2D is an one dimensional array:
The number of elements in the array at input Array2D is yielded at output NrOfElem1.
Value 1 is returned at output NrOfElem2.
If input Array2D is a two dimensional array:
From the array at input Array2D, the number of elements in the first dimension is yielded at output NrOfElem1 and the number of elements in the second dimension at output NrOfElem2.
If input Array2D is a three dimensional array:
From the array at input Array2D, the number of elements in the first dimension is yielded at output NrOfElem1 and the product of the array’s second and third dimensions at output NrOfElem2.
Hence, functions and function blocks can be written that process arrays of various lengths (also in conjunction with the functions (GetPointer, AreaOffs_ToVar,Var_ToAreaOffs).
The product NrOfElem1*NrOfElem2 always equals the total number of the elements in the array.
All input and output variables used for programming this function have been declared in the POU header. The same POU header is used for all programming languages.
VAR
ArrayOfInt: ARRAY [2..15,3..20] OF INT:=[252(0)];
NumberOfDim1: INT:=0;
NumberOfDim2: INT:=0;
END_VAR
In this case the function Elem_OfArray2D is executed in each CPU cycle (no EN input). It determines the number of elements of the variable ArrayOfInt. The result here is: NumberOfDim1 = 14 and NumberOfDim2 = 18.
BODY
WORKSPACE
NETWORK_LIST_TYPE := NWTYPELD ;
ACTIVE_NETWORK := 0 ;
END_WORKSPACE
NET_WORK
NETWORK_TYPE := NWTYPELD ;
NETWORK_LABEL := ;
NETWORK_TITLE := ;
NETWORK_HEIGHT := 5 ;
NETWORK_BODY
B(B_VARIN,,ArrayOfInt,6,2,8,4,);
B(B_VAROUT,,NumberOfDim1,19,2,21,4,);
B(B_F,Elem_OfArray2D!,Instance,8,1,19,5,,?DArray2D?CNrOfElem1?CNrOfElem2);
B(B_VAROUT,,NumberOfDim2,19,3,21,5,);
L(1,0,1,5);
END_NETWORK_BODY
END_NET_WORK
END_BODY
Further example projects (directory "Samples" of FPWIN Pro installation):Calculate the product of two-dimensional arrays