//Define a function that takes a partially filled array of numbers
//as its argument and returns the standard deviation of the numbers in
//the partially filled array. Since the partially filled array requires
//two arguments, the function will have two formal parameters: an array
//parameter and a formal parameter of type int that gives the number of the
//array positions used. The numbers in the array will be double. The task is
//the standard deviation of a list of numbers is a measure of how much the number
//deviate from the average. If the standard deviation is small the numbers are
//clustered close to the average and if the standard deviation is large, the numbers
// are scattered far from the average. The standard deviation, S,of a list of N
//numbers x, is defined as follows: where x is the average of N.

S = think square root symbol, N, sum(x, -x)to second power, i = 1, over N

I have not started the code yet as I have no real idea of what this wants .... if it could be explained or shown to me of what is required then I can start building code for this .... does anyone understand this enough to help me...

Dani AI

Generated

The task is: a function that accepts a partially filled array (pointer) and the count of used elements, then returns the standard deviation. was right about the basic signature idea; and covered the conceptual pieces, and correctly raised the population vs sample denominator issue. Below is a concise, robust approach that fills the gaps most beginners hit: numerical stability, input validation, and a clear choice between population and sample variance.

Welford’s one-pass algorithm computes mean and the sum of squared deviations in a single loop and avoids catastrophic cancellation that can happen with the naive two-pass method. Use a wider type (long double) for the running accumulators, check inputs (null pointer, nonpositive used count, and sample requires at least two values), and call std::sqrt for the final root.

#include <cmath>
#include <limits>

double standard_deviation(const double* data, int used, bool sample = false) {
    if (!data || used <= 0) return std::numeric_limits<double>::quiet_NaN();
    if (sample && used < 2) return std::numeric_limits<double>::quiet_NaN();

    long double mean = 0.0L;
    long double m2 = 0.0L; // sum of squares of differences
    for (int i = 0; i < used; ++i) {
        long double x = data[i];
        long double delta = x - mean;
        mean += delta / (i + 1);
        m2 += delta * (x - mean);
    }
    long double var = sample ? (m2 / (used - 1)) : (m2 / used);
    return std::sqrt(static_cast<double>(var));
}

Notes: prefer this over pow(x, 0.5) (as hinted) and choose the denominator based on whether the data represent the full population or a sample (as pointed out). For very large datasets or extreme values, keep intermediates in long double, and validate inputs before calling.

Recommended Answers

All 5 Replies

// ... the function will have two formal parameters: an array
//parameter and a formal parameter of type int that gives the number of the
//array positions used. The numbers in the array will be double. ...

Is
double func ( double *array, int numElements );

The standard deviaion is the square root of the sum of the variations from the average squared divided by the number of elements in the population.
numbers is an array of type double
N = number of elements ine numbers
x = average = sum of all elements in numbers divided by N
variance = numbers - x
variance squared = variance * variance, or you can use some other technique to to get this
sum = sum of variances squared
S= standard deviation = square root of (sum/N)

in C/C++ the square root of a number can be calculated using the pow() using 0.5 as the second parameter.

in C/C++ the square root of a number can be calculated using the pow() using 0.5 as the second parameter.

Isn't sqrt() available?

I apologize about leaving that option out. When thinking about a number to the n power I just automatically think of pow(). Duh...

S= standard deviation = square root of (sum/N)

FWIW, and I don't think it affects it too much for OP's purposes, it should be N-1 instead of N since it's a sample from a population (unless you have enough data to justify a population). Not knowing the population gives you one less degree of freedom.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.