(3) Declare a vector of doubles. Give the fillVector() function the correct para
ID: 3765551 • Letter: #
Question
(3) Declare a vector of doubles. Give the fillVector() function the correct parameter (pass by value or by reference?). Then in the fillVector() function, add a line of code at the FIXME to store values from the user into the vector. Call the fillVector() function in main().
(4) In function meanVector(), calculate and return the mean (average) of vector's numbers. Call meanVector() from main().
(5) In varianceVector(), calculate and return the variance the vector's numbers, using the mean calculated above. Hint: Store the squared differences in a new vector and then use the mean function again to calculate the variance. Call varianceVector() in main().
(6) In standardDeviationVector(), calculate and return the standard deviation of the vector's numbers, using the variance calculated above. Call standardDeviationVector() in main().
DO NOT change any of the provided function headers other than adding the parameter to the fillVector (FIXME 3). Each of your functions will be tested to insure they work correctly.
Ex.
Please use C++
Explanation / Answer
// FIXME - VECTOR LIBRARY
#include <cmath>
#include <iostream>
#include<vector>
using namespace std;
void fillVector(vector<double> &v) //(3) FIXME - Add Parameter
{
double userDouble = 0.0;
cout << "Enter numbers to populate the vector "<<endl;
cout << "(enter any other character to end user input): "<<endl;
while (cin >> userDouble) {
// cout<<userDouble<<endl;
v.push_back(userDouble);
}
//cout<<values[0]<<values[1]<<values[2];
cout << endl;
}
double meanVector(const vector <double> &v) {
//(4) FIXME - Calculate mean of vector numbers
double total=0.0;
int i=0;
for(i=0;i<v.size();i++)
{total=total+v[i];
}
return total/(v.size());
}
double varianceVector(double vectorAvg, const vector <double> &v) {
//(5) FIXME - Calculate variance of vector numbers
double total=0.0;
int i=0;
for(i=0;i<v.size();i++)
{
total=total+((v[i]-vectorAvg)*(v[i]-vectorAvg));
}
return total/(v.size());
}
double standardDeviationForVariance(double vectorVariance) {
//(6) FIXME - Calculate standard deviation
return sqrt(vectorVariance);
}
int main() {
//(3) FIXME - Declare vector of doubles
vector<double>values;
double avg = 0.0;
double var = 0.0;
double stdDev = 0.0;
unsigned int i = 0;
//(3) FIXME - Call fillVector() function
fillVector(values);
cout << "You entered: ";
//(3) FIXME - Print vector values (use i variable)
for(i=0;i<values.size();i++)
cout<<values[i]<<" ";
cout<<endl;
//(4) FIXME - Call meanVector() function
avg=meanVector(values);
//(5) FIXME - Call varianceVector() function
var=varianceVector(avg,values);
//(6) FIXME - Call standardDeviationForVariance() function
stdDev=standardDeviationForVariance(var);
cout << "The mean is: " << avg << endl;
cout << "The variance is: " << var << endl;
cout << "The standard deviation is: " << stdDev << endl;
system("PAUSE");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.