C code ONLY please Write a program that computes the central
C++ code ONLY please! Write a program that computes the central tendencies mean, mode, median and the variation: variance, standard deviation and range for a set of numbers given in the data file The output should be to two decimal places
Mean xxx.xx
Variance xxx.xx
Median xxx.xx
Std. Dev xxx.xx
Mode xxx.xx
Range xxx.xx
17.6 10.4 13.5 4 19.9 16 12 12.2 11.4 12.7 3 10.3 21.4 19.4 9 6.5 10.1 8.7 9.7 6.4 9.7 63 15.5 10.7 18.2 7.5 6.1 6.7 6.9 0.8 73.5 12 28 12.6 9.4 6.2 15.3 7.3 10.7 15.9 5.8 1 8.6 1.3 13.7 2.8 2.4 1.4 2.9 13.1 15.3 9.2 11.7 4.5 1 1.2 0.8 1 2.4 4.4 2.2 2.9 3.6 2.5 1.8 5.9 2.8 1.7 4.6 5.4 3 3.1 1.3 2.6 1.4 2.3 1 5.4 1.8 2.6 3.4 1.4 10.7 18.2 7.7 6.5 12.2 10.1 6.4 10.7 6.1 0.8 12 28.1 9.4 6.2 7.3 9.7 62.1 15.5 6.4 9.5
Solution
#include<iostream>
#include<fstream>
#include<string>
#include<stdlib.h>
#include<cmath>
using namespace std;
float data[200];
int n; // number of elements
float std_dev, average ;
void std_deviation(float var);
float variance()
{
int i;
float sum = 0, sum1=0, var;
for (i = 0; i < n; i++)
{
sum = sum + data[i];
}
average = sum / (float)n; // average means Mean
/* Compute variance and standard deviation */
for (i = 0; i < n; i++)
{
sum1 = sum1 + pow((data[i] - average), 2);
}
var = sum1 / (float)n;
std_deviation(var);
return var;
}
void std_deviation(float var)
{
std_dev = sqrt(var);
}
float Median() {
float* sorted = new float[n];
for (int i = 0; i < n; ++i) {
sorted[i] = data[i];
}
for (int i = n - 1; i > 0; --i) {
for (int j = 0; j < i; ++j) {
if (sorted[j] > sorted[j+1]) {
float dTemp = sorted[j];
sorted[j] = sorted[j+1];
sorted[j+1] = dTemp;
}
}
}
float median = 0.0;
if ((n % 2) == 0) {
median = (sorted[n/2] + sorted[(n/2) - 1])/2.0;
} else {
median = sorted[n/2];
}
delete [] sorted;
return median;
}
float Mode() {
int* repeat = new int[n];
for (int i = 0; i < n; ++i) {
repeat[i] = 0;
int j = 0;
bool bFound = false;
while ((j < i) && (data[i] != data[j])) {
if (data[i] != data[j]) {
++j;
}
}
++(repeat[j]);
}
int MaxRepeat = 0;
for (int i = 1; i < n; ++i) {
if (repeat[i] > repeat[MaxRepeat]) {
MaxRepeat = i;
}
}
delete [] repeat;
return data[MaxRepeat];
}
float Range()
{
float highest=data[0], range;
float lowest=data[0];
for(int i=1;i<n;i++)
{
if(highest<data[i])
highest = data[i];
if(lowest>data[i])
lowest = data[i];
}
range = highest - lowest;
return range;
}
int main()
{
char word[10];
int i=0;
ifstream infile(\"datafile\");
while(infile >> word)
{
data[i++] = atof(word);
}
n = i;
cout<<\"Variance : \"<<variance()<<\"\ \";
cout<<\"Mean : \"<<average<<\"\ \";
cout<<\"Mode : \"<<Mode()<<\"\ \";
cout<<\"Median : \"<<Median()<<\"\ \";
cout<<\"std_deviation : \"<<std_dev<<\"\ \";
cout<<\"Range : \"<<Range()<<\"\ \";
}


