Total and mean numbers calculation in 16-bit data table
This function calculates the sum and the arithmetic mean of numbers (both with +/- signs) in the specified 16-bit data table.
Input
starting area of data table
ending area of data table
Output
sum of all elements in data table area specified
mean of all elements in data table area specified
Input s1_Start specifies the starting area of the data table, and s2_End specifies the end. The sum of all elements in the data table is returned at output Sum and the arithmetic mean of all elements in the data table is returned at output Mean. The arithmetic mean is rounded off if it is not a whole number.
if the area specified using the index modifier exceeds the limit.
if the address of the variable at input s1_Start > s2_End
if s1_Start and s2_End belong to different data areas.
if the area specified using the index modifier exceeds the limit.
if the address of the variable at input s1_Start > s2_End
if s1_Start and s2_End belong to different data areas.
if the total value range overflows or underflows the 16-bit range.
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
start: BOOL:=FALSE;
(*activates the function*)
data_array: ARRAY [0..4] OF INT:=[4,7,-32,13,5];
sum: DINT:=0;
(*the sum of all elements of
data_array; here: -3*)
mean: INT:=0;
(*the arithmetic mean of all
elements of data_array;
here: 0*)
END_VAR
When the variable start is set to TRUE, the function is carried out. The function calculates the sum of all elements of the data table (sum = 4 + 3 + 8 + (-2) + 1 + (-6) = 8
) and writes the result (in this case 8) to the variable sum. Additionally, the function calculates the arithmetic mean of all elements of the data table (mean = sum/6 = (4 + 3 + 8 + (-2) + 1 + (-6)) / 6 = 1.333
) and writes the rounded-off number (in this case 1) to the variable mean.
BODY
WORKSPACE
NETWORK_LIST_TYPE := NWTYPELD ;
ACTIVE_NETWORK := 0 ;
END_WORKSPACE
NET_WORK
NETWORK_TYPE := NWTYPELD ;
NETWORK_LABEL := ;
NETWORK_TITLE := ;
NETWORK_HEIGHT := 6 ;
NETWORK_BODY
B(B_VARIN,,output,8,1,10,3,);
B(B_VARIN,,data field[0],8,2,10,4,);
B(B_VARIN,,data field[4],8,3,10,5,);
B(B_F,F275_MEAN,,10,0,19,5,,?DEN?D@'s1'?Ds2?AENO?Csum?Cmean);
B(B_VAROUT,,sum,19,2,21,4,);
B(B_VAROUT,,mean,19,3,21,5,);
L(1,0,1,6);
END_NETWORK_BODY
END_NET_WORK
END_BODY
IF start then
F275_MEAN( s1_Start:= data_field[0],
s2_End:= data_field[4],
Sum=> sum,
Mean=> mean);
END_IF;