Thursday, July 12, 2018

Computing Sum, Mean, Variance, Standard Deviation, Coefficient of Variation, Smallest, Biggest, Median, Range, and Mode using C++

Standard Deviation Input

This is a programming tutorial for computing sum, mean, variance, standard deviation, coefficient of variation, smallest number, biggest number, median, range, and mode of n numbers using C++. In this tutorial, I have used Dev C++ v5.11 software for compiling the C++ program.

The formula for calculating sum, mean, variance, standard deviation, coefficient of variation, smallest number, biggest number, median, range, and mode of set of n numbers is given below:

Description Formula
Number of Samples n
Sum (Total) \[\sum\limits_{i=1}^{n}{{{x}_{i}}}={{x}_{1}}+{{x}_{2}}+...+{{x}_{n}}\]
Mean (Average) \[\bar{x}=\frac{\sum\limits_{i=1}^{n}{{{x}_{i}}}}{n}\]
Variance \[{{\operatorname{var}}_{x}}=\frac{\sum\limits_{i=1}^{n}{{{({{x}_{i}}-\bar{x})}^{2}}}}{n-1}\]
Standard Deviation \[{{\sigma }_{x}}=\sqrt{\frac{\sum\limits_{i=1}^{n}{{{({{x}_{i}}-\bar{x})}^{2}}}}{n-1}}\]
Coefficient of Variance \[c{{v}_{x}}=\frac{{{\sigma }_{x}}}{{\bar{x}}}\times 100%\]
Minimum (Smallest Number) x1
Maximum (Biggest Number) xn
Median (Middle) \[m{{d}_{x}}={{x}_{\left( \frac{n-1}{2} \right)}},\ if\ n\ is\ odd\]
\[m{{d}_{x}}=\frac{{{x}_{\left( \frac{n}{2} \right)}}+{{x}_{\left( \frac{n}{2}+1 \right)}}}{2},\ if\ n\ is\ even\]
Mode (Most Common) frequencymax

Source Code

// Sum, Mean, Variance, Standard Deviation, Coefficient of Variation, Smallest, Biggest, Median, Range, and Mode.
#include <iostream>
#include <sstream>
#include <string>
#include <conio.h>
#include <math.h>
#include <iomanip>
using namespace std;
string n2s(double y)
{
  std::stringstream p;
  std::string q;
  p << y;
  p >> q;
  return q;
}
int main()
{
  int i, j, n, x, max, mn;
  double t, a[20], b[20], s[20], s1, z, m, cv, dev, var, sum, mean, median, range, sd;
  string mode;
  sum = x = dev = max = mn = range = 0;
  mode = "";
  
  cout << "\nSum, Mean, Variance, Standard Deviation, Coefficient of Variation, Smallest, Biggest, Median, Range, and Mode\n\n";
  cout << "\nHow many numbers? ";
  cin >> n;
  cout << "\nEnter the numbers: ";
  for (i = 0; i < n; i++) {
    cin >> a[i];
    s[i] = a[i];
    sum += a[i];
  }
  mean = sum / n;
  system("cls");
  cout << "\nInput Numbers: ";
  for (i = 0; i < n; i++) {
    i < n-1 ? cout << a[i] << ", " : cout << "and " << a[i] << endl;
    dev += pow((a[i] - mean), 2);
  }
  var = dev / (n - 1);
  sd = sqrt(var);
  cv = (sd / mean) * 100;
  for (i = 0; i < n; i++) {
    for (j = i + 1; j < n; j++) {
      if (s[i] > s[j]) {
        t = s[i];
        s[i] = s[j];
        s[j] = t;
      }
    }
  }
  s1 = s[0];
  cout << "\nSorted Numbers: ";
  for(i = 0; i < n; i++) {
    i < n-1 ? cout << s[i] << ", " : cout << "and " << s[i] << endl;
    b[i+1] = s[i];
  }
  range = s[n-1] - s[0];
  cout << "\n" << setw(29) << "Total Numbers : " << n << endl;
  cout << "\n" << setw(29) << "Sum (Total) : " << sum << endl;
  cout << "\n" << setw(29) << "Mean (Average) : " << mean << endl;
  cout << "\n" << setw(29) << "Variance (\xE5\xFD) : " << var << endl;
  cout << "\n" << setw(29) << "Standard Deviation : " << sd << endl;
  cout << "\n" << setw(29) << "Coefficient of Variation : " << cv << " \%" << endl;
  if (n%2 == 0) {
    median = (b[n/2] + b[(n/2)+1]) / 2;
  } else {
    median = b[(n+1)/2];
  }
  cout << "\n" << setw(29) << "Smallest Number : " << s[0] << endl;
  cout << "\n" << setw(29) << "Biggest Number : " << s[n-1] << endl;
  cout << "\n" << setw(29) << "Median (Middle) : " << median << endl;
  cout << "\n" << setw(29) << "Range (Biggest - Smallest) : " << range << endl;
  cout << "\n" << setw(29) << "Mode (Most Common) : ";
  for (i = 0; i <= n; i++) {
    z = s[i];
    if (z == s1) {
      x += 1;
      if (x > max) {
        m = s1;
        max = x;
        mn++;
        mode = n2s(m);
      }
    } else {
      if (x == max &&  m != s1) {
        mode += ", " + n2s(s1);
        mn++;
      }
      s1 = z;
      x = 1;
    }
  }
  if (n == mn) {
    cout << "No Mode Found" << endl;
  } else {
    cout << mode << endl;
  }
  getch();
}

Output

Standard Deviation Output

0 comments :

Post a Comment