In: Computer Science
Write a program in C++
Write three new functions, mean() and standard_deviation(). Modify your code to call these functions instead of the inline code in your main().
In addition, add error checking for the user input. If the user inputs an incorrect value prompt the user to re-enter the number.
The flow of the code should look something like:
/** Calculate the mean of a vector of floating point numbers **/ float mean(vector& values) /** Calculate the standard deviation from a vector of floating point numbers **/ float standard_deviation(vector& values) /** Main **/ int main() { /** Prompt the user and get the number of values to enter **/ /** Get the values from the user **/ /** Display the mean and standard deviation **/ return 0; }
taking the condition only floating point number are using in the vector so the code is as follows
// including required libraries
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <cmath>
using namespace std;
// Calculate the mean of a vector of floating point numbers
void mean(vector<float> vect)
{
float sum,mean;
int n=vect.size();
for(int i=0;i<n;i++)
{
sum = sum+vect[i];
}
mean=sum/n;
cout<< "\nmean of the given vector is: "<<
mean<<"\n";
}
// Calculate the standard deviation from a vector of floating point
numbers
void standard_deviation(vector<float> vect)
{
float sum = 0.0, mean, variance = 0.0,var, sd;
int n=vect.size();
for(int i=0;i<n;i++)
{
sum = sum+vect[i];
}
mean=sum/n;
for(int i=0;i<n;i++)
{
variance =
variance+pow(vect[i]-mean,2) ;
}
var=variance/n;
sd=sqrt(var);
cout<< "\nStandard deviation
of the given vector is: "<< sd<<"\n";
}
int main()
{
//intializing variables
int n,i;
float a;
float b=4.5656;
//intializing vector
vector<float> vect;
//taking length of the vector
cout<< "Enter length of the vector: ";
cin>>n;
// taking values into the vector
cout<< "Enter values in the vector\n";
for(i=0;i<n;i++)
{
cout<<
"vect["<<i<<"]= ";
cin>>a;
if(floor(a)-a==0)// checking the given number is
float or not
{
cout<< "please enter
valid float number : "; //error condition if not
float
cin>>a;
vect.push_back(a);
}
else{
vect.push_back(a);
}
}
//calling function mean
mean(vect);
//calling function standard deviation
standard_deviation(vect);
return 0;
}
OUTPUT
also uploading images of code in case of any error in written code please go through the code in images
any queries comment please:)