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. 


Expert's answer

#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!

LATEST TUTORIALS
APPROVED BY CLIENTS