Question #185514

Write a function that uses the formulas below

𝑚𝑒𝑎𝑛 = (𝑥1 + 𝑥2 + ⋯ + 𝑥𝑁)/𝑁

𝑣𝑎𝑟𝑖𝑎𝑛𝑐𝑒 =

𝑠(𝑥1 − 𝑥̅)2 + (𝑥2 − 𝑥̅)2 + ⋯ + (𝑥𝑁 − 𝑥̅)2

𝑁

𝑠𝑑 = √𝑣𝑎𝑟𝑖𝑎𝑛𝑐𝑒

a. Returns the mean (i.e. average) 𝑥̅, of the values in an array by value.

b. Returns the variance of the data set by reference.

c. Returns the standard deviation of the data set by reference. 


1
Expert's answer
2021-05-03T03:32:02-0400
#include <iostream>


float get_mean(float* arr, int n) {
    float mean{ 0 };
    for (int i = 0; i < n; i++) {
        mean += arr[i];
    }
    return mean/n;
}
float get_variance(float* arr, int n) {
    float variance{ 0 };
    float mean = get_mean(arr, n);
    for (int i = 0; i < n; i++) {
        variance += pow((arr[i] - mean), 2);
    }
    return variance/n;
}


float get_deviation(float* arr, int n) {
    float deviation{ 0 };
    deviation = pow(get_variance(arr, n), 0.5);
    return deviation;
}


int main()
// simple terst function
{
    const int n = 10;
    float user_array[10] = { 1,2,3,4,5,6,7,8,9,10 };
    std::cout << "Mean: " << get_mean(user_array, n) << std::endl;
    std::cout << "Variance: " << get_variance(user_array, n) << std::endl;
    std::cout << "Deviation: " << get_deviation(user_array, n) << std::endl;
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!
LATEST TUTORIALS
APPROVED BY CLIENTS