Write a program that computes the central tendencies mean (average), mode, median and the variation: variance, standard deviation and range for a set of numbers given in the data file ArsenicGroundWaterFile.txt.
The following data represent (naturally occurring) concentration of arsenic in ground water for a random sample of 102 Northwest Texas wells. Units are parts per billion.
Reference: Nichols, C.E. and Kane, V.E., Union Carbide Technical Report K/UR-1
The output should be as follows to two decimal places:
Central Tendency Variation
Mean xxx.xx Variance xxx.xx
Median xxx.xx Std. Dev xxx.xx
Mode xxx.xx Range xxx.xx
I have no idea how to do this assignment, my professor hasn't been of any help. Please help me get started.
#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdlib>
#include <vector>
usingnamespace std;
int main()
{
// Open file and check it is open
ifstream myfile("numbers.txt");
if (!myfile)
{
cout << "could not open input file\n";
return 1;
}
// read all values from file into a vector
vector<double> data;
double value;
while (myfile >> value)
{
data.push_back(value);
}
// Output contents of vector
cout << "Number of values: " << data.size() << '\n';
for (size_t i = 0; i < data.size(); i++)
{
cout << data[i] << endl;
}
}
2. same functionality (more or less) using an array instead:
I added more comments because arrays require more care and attention.
#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdlib>
usingnamespace std;
int main()
{
// Open file and check it is open
ifstream myfile("numbers.txt");
if (!myfile)
{
cout << "could not open input file\n";
return 1;
}
// Allocate an array - estimate size required
constint SIZE = 1000; // Maximum number of values
double data[SIZE]; // define an array we hope is large enough
int count = 0; // keep track of how many values are stored.
// read all values from file into an array
double value;
while (count < SIZE && myfile >> value) // while array is not full, read a value
{
data[count] = value; // store it
count++; // increaase count
}
// Output contents of array
cout << "Number of values: " << count << '\n';
for (int i = 0; i < count; i++)
{
cout << data[i] << endl;
}
}
Once you reach this stage, it's time to move on to writing separate functions to generate the each of the various statistics required.