Answer to Question #185514 in C++ for Woyengimiesindo

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!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS