user40030: Looking for a simple way to get the variance of an array of numbers,
that will compile with the Arduino compiler.
Something like this c#:
static float variance(double []a, int n)
{
    // Compute mean (average of elements)
    double sum = 0;

    for (int i = 0; i < n; i++) sum += a[i];
    double mean = (double)sum / (double)n;
    // Compute sum squared differences with mean.
    double sqDiff = 0;
    for (int i = 0; i < n; i++)
        sqDiff += (a[i] - mean) * (a[i] - mean);
    return (float)sqDiff / n;
}


// Driver Code
public static void Main ()
{
    double []arr = {10, 11, 12, 13};
    int n = arr.Length;
    Console.WriteLine( "Variance: " + variance(arr, n));
}


To keep it simple, and thanks for all your help....
it is a platform with actuators in each corner to raise and lower
the platform....
Each actuator sends a feed back via modbus to the main micro processor at its request
Which is stored to a array, which is processed and then sent via modbus
Back to control (simple on/off) each actuator to keep platform in somewhat of a level position +/- a set number....
I already use the min/max  (max – min =  ) in the my code to limit a set, simple differential between the
Actuators....  I am thinking that using the variance differential would work better for the application....
Thinking it would also detect a jammed or faulty actuator faster than the max/min difference with
Less platform twisting....
(Response to Edgar), You could look at it as XYZ,…  X= position.. Y= actuator.. Z=sample rate
This is my code snip it for the max/min
example 1  —  adj_str =  actuators position = (10,11,10,9) run level flag 1
example 2  —  adj_str =  actuators position = (10,14,10,9) run level flag 0
void min_max()
{
    int max = adj_str[0];
    int min = adj_str[0];
    for (int i = 0; i <= 3; i++)
    {
        const int value = adj_str[i];
        if (value > max) max = value ;
        if (value < min) min = value ;
    }
    //-
    adj_str_dif = max - min;
    //-
    if (adj_str_dif < 1) level_flag = 1;
    //-
    if ((level_flag == 1)&&(adj_str_dif > 4))  // 4 = the differential
    { level_flag = 0;}
    //-
    if (level_flag == 0)           // Level Till Max or Min (re-level)
    {
        // -- Lower --
        if (adj_str_dif > 0 && (digitalRead(but_pin[1]) == LOW))
            base = (max);
        // -- Raise --
        if (adj_str_dif > 0 && (digitalRead(but_pin[2]) == LOW))
            base = (min);
    }
    //-
    if (level_flag == 1)       // Run Till full retract or full extend
    {
        // -- Lower --
        if (adj_str_dif < 1 && (digitalRead(but_pin[1]) == LOW))
            base = 1023;
        // -- Raise --
        if (adj_str_dif < 1 && (digitalRead(but_pin[2]) == LOW))
            base = 0;
    }
}


user37622: C++ and C# (or Java) basic language constructs are very similar. Here is the Arduino version of your C# code with minimal required modifications. There is no change in the function implementation.

float variance(double a[], int n) 
{   
    // Compute mean (average of elements) 
    double sum = 0; 
    
    for (int i = 0; i < n; i++) sum += a[i];    
    double mean = (double)sum / (double)n; 
    // Compute sum squared differences with mean. 
    double sqDiff = 0; 
    for (int i = 0; i < n; i++) 
        sqDiff += (a[i] - mean) * (a[i] - mean); 
    return (float)sqDiff / n; 
} 


void setup() {
    Serial.begin(115200);
    double arr[] = {10, 11, 12, 13}; 
    int n = sizeof(arr) / sizeof(double); 
    Serial.print( "Variance: ");
    Serial.println(variance(arr, n));                            
}

void loop() {

}


40030: <p>Looking for a simple way to get the variance of an array of numbers,
that will compile with the Arduino compiler.
Something like this c#:</p>
<pre><code>static float variance(double []a, int n)
{
    // Compute mean (average of elements)
    double sum = 0;

    for (int i = 0; i &lt; n; i++) sum += a[i];
    double mean = (double)sum / (double)n;
    // Compute sum squared differences with mean.
    double sqDiff = 0;
    for (int i = 0; i &lt; n; i++)
        sqDiff += (a[i] - mean) * (a[i] - mean);
    return (float)sqDiff / n;
}


// Driver Code
public static void Main ()
{
    double []arr = {10, 11, 12, 13};
    int n = arr.Length;
    Console.WriteLine( &quot;Variance: &quot; + variance(arr, n));
}
</code></pre>
<hr />
<p>To keep it simple, and thanks for all your help....
it is a platform with actuators in each corner to raise and lower
the platform....
Each actuator sends a feed back via modbus to the main micro processor at its request
Which is stored to a array, which is processed and then sent via modbus
Back to control (simple on/off) each actuator to keep platform in somewhat of a level position +/- a set number....
I already use the min/max  (max – min =  ) in the my code to limit a set, simple differential between the
Actuators....  I am thinking that using the variance differential would work better for the application....
Thinking it would also detect a jammed or faulty actuator faster than the max/min difference with
Less platform twisting....
(Response to Edgar), You could look at it as XYZ,…  X= position.. Y= actuator.. Z=sample rate</p>
<p>This is my code snip it for the max/min</p>
<p>example 1  —  adj_str =  actuators position = (10,11,10,9) run level flag 1</p>
<p>example 2  —  adj_str =  actuators position = (10,14,10,9) run level flag 0</p>
<pre class="lang-cpp prettyprint-override"><code>void min_max()
{
    int max = adj_str[0];
    int min = adj_str[0];
    for (int i = 0; i &lt;= 3; i++)
    {
        const int value = adj_str[i];
        if (value &gt; max) max = value ;
        if (value &lt; min) min = value ;
    }
    //-
    adj_str_dif = max - min;
    //-
    if (adj_str_dif &lt; 1) level_flag = 1;
    //-
    if ((level_flag == 1)&amp;&amp;(adj_str_dif &gt; 4))  // 4 = the differential
    { level_flag = 0;}
    //-
    if (level_flag == 0)           // Level Till Max or Min (re-level)
    {
        // -- Lower --
        if (adj_str_dif &gt; 0 &amp;&amp; (digitalRead(but_pin[1]) == LOW))
            base = (max);
        // -- Raise --
        if (adj_str_dif &gt; 0 &amp;&amp; (digitalRead(but_pin[2]) == LOW))
            base = (min);
    }
    //-
    if (level_flag == 1)       // Run Till full retract or full extend
    {
        // -- Lower --
        if (adj_str_dif &lt; 1 &amp;&amp; (digitalRead(but_pin[1]) == LOW))
            base = 1023;
        // -- Raise --
        if (adj_str_dif &lt; 1 &amp;&amp; (digitalRead(but_pin[2]) == LOW))
            base = 0;
    }
}
</code></pre>
 37622: <p>C++ and C# (or Java) basic language constructs are very similar. Here is the Arduino version of your C# code with minimal required modifications. There is no change in the function implementation.</p>
<pre><code>
float variance(double a[], int n) 
{   
    // Compute mean (average of elements) 
    double sum = 0; 
    
    for (int i = 0; i &lt; n; i++) sum += a[i];    
    double mean = (double)sum / (double)n; 
    // Compute sum squared differences with mean. 
    double sqDiff = 0; 
    for (int i = 0; i &lt; n; i++) 
        sqDiff += (a[i] - mean) * (a[i] - mean); 
    return (float)sqDiff / n; 
} 


void setup() {
    Serial.begin(115200);
    double arr[] = {10, 11, 12, 13}; 
    int n = sizeof(arr) / sizeof(double); 
    Serial.print( &quot;Variance: &quot;);
    Serial.println(variance(arr, n));                            
}

void loop() {

}
</code></pre>
