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.Β
#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;
}
Comments
Leave a comment