Some Statistics This assignment should utilize the vector container of the STL.
ID: 3606336 • Letter: S
Question
Some Statistics
This assignment should utilize the vector container of the STL. All functions should use either iterators or the range-based for when accessing or traversing vectors.
double calcMean (const vector<int>&);
The mean or average of a group of N numbers x1, x2, ... xn is the sum of the numbers divided by how many there are; it is represented by x(bar).
mean = x(bar) = 1/N sum(i=1 to N) xi
double calcStandardDeviation (const vector<int>&);
The standard deviation of the same group of N numbers is given by the following formula.
standardDeviation = sqrt(1/N-1) sum(i=1 to N) (xi - x(bar))2
Note that function standardDeviation should in turn call function mean in order to implement its formula above.
void displayValues(const vector<int>&);
Display the contents of the vector to the screen 16 numbers per line with all numbers shown in columns of width 5.
int findMaximum(const vector<int>&);
Find Maximum Number in an Vector
int findMinimum(const vector<int>&);
Similar to the findMaximum function except it finds the smallest value in the vector.
int findSecondMaximum(const vector<int>&);
Similar to the findMaximum function except it finds the second largest value in the vector.
int findSecondMinimum(const vector<int>&);
Similar to the findMinimum function except it finds the second smallest value in the vector.
void writeData(string, const vector<int>&);
The data contained in the vector is stored in the filename specified in the string. If a file with that filename already exists, it should be overwritten. The data should be stored in the file one number per line.
vector<int> readValues(string);
Data contained in the file indicated by the filename specified in the string should be read into a vector. The function should return this vector to the calling function.
bool findMostExtremeOutlier(const vector<int>&, int&);
In statistics, an outlier is an observation point that is distant from other observations. An outlier may be due to variability in the measurement or it may indicate experimental error; the latter are sometimes excluded from the data set. (from Wikipedia.org)
The function determines the most extreme outlier, if one exists. If an outlier is found, the function should store the value of the most extreme outlier in the supplied reference parameter and return true to the calling function. If an outlier is not found, the function should simply return false to the calling function.
Use the following to determine if a data value is an outlier:
| x - x(bar) | > 3*sigma
where sigma is the standard deviation.
The most extreme outlier, if any outliers exist, will be the outlier with the most extreme distance from the mean.
vector<int> removeMostExtremeOutlier(const vector<int>&);
The function should create a new vector with the most extreme outlier removed from the data. The function should return the resulting vector to the calling function.
int main()
Arrange function main so that it immediately reads a set of values from the file vectorTestData.txt using the readValues function; it should then present the user repeatedly with the following options:
find maximum
find 2nd maximum
find minimum
find mean
find standard deviation
display most extreme outlier
remove most extreme outlier and recalculate standard deviation
display values
read a new set of values
exit program
When the user chooses to
read a new set of values
s/he should be prompted for the name of the file containing the values to be read
remove most extreme outlier and recalculate standard deviation
the following actions should be taken
call removeMostExtremeOutlier to remove the most extreme outlier
alter the filename by adding sansOutlier- to the beginning of the existing filename (so data.txt would become sansOutlier-data.txt)
write the new data set to a file using the new filename
call calcStandardDeviation to calculate the standard deviation using the new data and display it to the user
Please give answer in C++ language
Create a new plain text document, vectorTestData.txt, containing several integers, one number per line. Example data follows.Explanation / Answer
#include<bits/stdc++.h>
#include<fstream>
using namespace std;
double calcMean(const vector<int>& vec)
{
int n=vec.size();
int sum=0;
for(int i=0;i<n;i++)
{
sum=sum+vec[i];
}
return sum/n;
}
double calcStandardDeviation(const vector<int>& vec)
{
int mean=calcMean(vec);
int n=vec.size();
int sum=0;
for(int i=0;i<n;i++)
{
sum=sum+(vec[i]-mean)*(vec[i]-mean);
}
return sum/(n-1);
}
void displayValues(const vector<int>& vec)
{
int n=vec.size();
for(int i=0;i<n;i++)
{
cout<<setw(5)<<left<<vec[i];
if(i%15==0)
{
cout<<endl;
}
}
}
int findMaximum(const vector<int>& vec)
{
int maxi=vec[0];
int n=vec.size();
for(int i=1;i<n;i++)
{
if(maxi<vec[i])
{
maxi=vec[i];
}
}
return maxi;
}
int findSecondMaximum(const vector<int>& vec)
{
int max1,max2;
if(vec[0]<vec[1])
{
max1=vec[1], max2=vec[0];
}
else
{
max1=vec[0], max2=vec[1];
}
int n=vec.size();
for(int i=2;i<n;i++)
{
if(max1<vec[i])
{
max2=max1;
max1=vec[i];
}
else if(max2<vec[i])
{
max2=vec[i];
}
}
return max2;
}
int findMinimum(const vector<int>& vec)
{
int mini=vec[0];
int n=vec.size();
for(int i=1;i<n;i++)
{
if(mini>vec[i])
{
mini=vec[i];
}
}
return mini;
}
int findSecondMinimum(const vector<int>& vec)
{
int min1,min2;
if(vec[0]>vec[1])
{
min1=vec[1], min2=vec[0];
}
else
{
min1=vec[0], min2=vec[1];
}
int n=vec.size();
for(int i=2;i<n;i++)
{
if(min1>vec[i])
{
min2=min1;
min1=vec[i];
}
else if(min2>vec[i])
{
min2=vec[i];
}
}
return min2;
}
void writeData(string filename, const vector<int>& vec)
{
ofstream myfile (filename);
int n=vec.size();
if (myfile.is_open())
{
for(int i = 0; i < n; i ++){
myfile << vec[i] << endl ;
}
myfile.close();
}
else cout << "Unable to open file";
}
vector<int> readValues(string filename)
{
vector<int> vec;
ifstream myfile(filename, ios::in);
int n,tmp;
myfile >> n;
for(int i = 0; i < n; i++) {
myfile >> tmp;
vec.push_back(tmp);
}
return vec;
}
bool findMostExtremeOutlier(const vector<int>& vec, int& outlier)
{
int n=vec.size();
int mean=calcMean(vec);
int sigma=calcStandardDeviation(vec);
int sigma3= 3*sigma;
int maxi=INT_MIN;
bool isExists=false;
for(int i=0;i<n;i++)
{
if(abs(vec[i]-mean)> sigma3)
{
isExists=true;
if(abs(vec[i]-mean)>maxi)
{
maxi=abs(vec[i]-mean);
outlier=vec[i];
}
}
}
return isExists;
}
vector<int> removeMostExtremeOutlier(const vector<int>& vec)
{
int outlier=-1;
bool isExists= findMostExtremeOutlier(vec,outlier);
if(!isExists)
{
return vec;
}
std::vector<int>::iterator position = std::find(vec.begin(), vec.end(), outlier);
if (position != vec.end())
vec.erase(position);
return vec;
}
int main()
{
int option=0;
vector<int> vec;
while(option!=11)
{
cout<<endl<<"Enter 1-find maximum 2-find 2nd maximum 3-find minimum 4-find 2nd minimum 5-find mean 6-find standard deviation
7-display most extreme outlier 8-remove most extreme outlier and recalculate standard deviation
9-display values 10-read a new set of values 11-exit program"<<endl;
cin>>option;
if(option==1)
{
findMaximum(vec);
}
else if(option==2)
{
findSecondMaximum(vec);
}
else if(option==3)
{
findMinimum(vec);
}
else if(option==4)
{
findSecondMinimum(vec);
}
else if(option==5)
{
calcMean(vec);
}
else if(option==6)
{
calcStandardDeviation(vec);
}
else if(option==7)
{
int outlier=0;
findMostExtremeOutlier(vec,outlier);
}
else if(option==8)
{
removeMostExtremeOutlier(vec);
}
else if(option==9)
{
displayValues(vec);
}
else if(option==10)
{
cout<<"Enter file name"<<endl;
string filename;
cin>>filename;
vec=readValues(filename);
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.