C++ Program to implement standard error of mean


In this tutorial, we will be discussing a program to implement standard error of mean.

Standard error of mean is the estimation of sample mean dispersion from population mean. Then it is used to estimate the approximate confidence intervals for the mean.

Example

#include <bits/stdc++.h>
using namespace std;
//calculating sample mean
float calc_mean(float arr[], int n){
   float sum = 0;
   for (int i = 0; i < n; i++)
      sum = sum + arr[i];
   return sum / n;
}
//calculating standard deviation
float calc_deviation(float arr[], int n){
   float sum = 0;
   for (int i = 0; i < n; i++)
      sum = sum + (arr[i] - calc_mean(arr, n)) * (arr[i] - calc_mean(arr, n));
   return sqrt(sum / (n - 1));
}
//calculating sample error
float calc_error(float arr[], int n){
   return calc_deviation(arr, n) / sqrt(n);
}
int main(){
   float arr[] = { 78.53, 79.62, 80.25, 81.05, 83.21, 83.46 };
   int n = sizeof(arr) / sizeof(arr[0]);
   cout << calc_error(arr, n) << endl;
   return 0;
}

Output

0.8063
raja
Published on 03-Dec-2019 10:51:09
Advertisements