computes the standard deviation1 of a list of numbers in a
file. The program is to be called comp201 stddev and will take one command line argument: the name
of the file containing the numbers.
You must implement the following functions:
void read_numbers(ifstream& f, vector<double>& numbers);
double compute_standard_deviation(vector<double> numbers);
The read numbers function takes in a reference to a file, as well as a reference to a vector of double
variables. The function must read all the numbers from the file into the vector.
The compute standard deviation function takes in a vector of numbers and must compute and return
the standard deviation. The standard deviation is the square root of the average of the squared
differences of the values from their average value. Whew – how about an example! Let’s say the file
had 5 numbers: 1, 3, 4.5, 5.5, 6.
First, find the average:
Next, calculate the difference
of each data point from the mean, and square the result of each:
Finally, take the square root of the mean of these
values:
read numbers function takes in a reference to a file, as well as a reference to a vector of double
variables. The function must read all the numbers from the file into the vector.
#include <fstream>
#include <vector>
usingnamespace std;
// Preconditions: f is a valid and open input file stream
// Postconditions: all the numbers in f are added to numbers
void read_numbers(ifstream& f, vector<double>& numbers);
// Preconditions: numbers is a vector of numbers
// Returns: the population standard deviation of the numbers array
double compute_standard_deviation(vector<double> numbers);
int main(int argc, char* argv[])
{
return 0;
}
The read numbers function takes in a reference to a file, as well as a reference to a vector of double
variables. The function must read all the numbers from the file into the vector.
// Preconditions: f is a valid and open input file stream
Your function has parameter named f. Why the line 3?
Your precondition states that the function expects an open stream. Why lines 4-9?
If your function does not open the stream, then why line 16?
#include <iostream>
#include <fstream>
#include <vector>
usingnamespace std;
// Preconditions: f is a valid and open input file stream
// Postconditions: all the numbers in f are added to numbers
void read_numbers(ifstream& f, vector<double>& numbers);
// Preconditions: numbers is a vector of numbers
// Returns: the population standard deviation of the numbers array
double compute_standard_deviation(vector<double> numbers);
int main(int argc, char* argv[])
{
string filename = argv[0];
ifstream fin(argv[0]);
vector<double> num;
read_numbers(fin, num);
compute_standard_deviation(num);
cout << compute_standard_deviation(num) << endl;
return 0;
}
void read_numbers(ifstream& f, vector<double>& numbers)
{
if (!f.is_open())
{
cout << "Couldn't open file." << endl;
exit(-1);
}
double digits;
while(!f.eof())
{
f >> digits;
numbers.push_back(digits);
cout << "num" << endl;
}
f.close();
}
double compute_standard_deviation(vector<double> numbers)
{
double sum = 0, average = 0;
for (unsignedint i = 0; i < numbers.size(); i++)
{
sum = sum + numbers[i];
}
average = sum / numbers.size();
sum = 0;
for (unsignedint i = 0; i < numbers.size(); i++)
{
sum += pow(numbers[i] - average, 2);
}
return sqrt(sum / numbers.size());
}
#include <iostream>
#include <fstream>
#include <vector>
usingnamespace std;
// Preconditions: f is a valid and open input file stream
// Postconditions: all the numbers in f are added to numbers
void read_numbers(ifstream& f, vector<double>& numbers);
// Preconditions: numbers is a vector of numbers
// Returns: the population standard deviation of the numbers array
double compute_standard_deviation(vector<double> numbers);
int main(int argc, char* argv[])
{
ifstream fin(argv[0]);
vector<double> num;
read_numbers(fin, num);
cout << compute_standard_deviation(num) << endl;
return 0;
}
void read_numbers(ifstream& f, vector<double>& numbers)
{
if (!f.is_open())
{
cout << "Couldn't open file." << endl;
exit(-1);
}
double digits;
while(f >> digits) //checks whether end-of-file associated with stream
{
numbers.push_back(digits);
}
f.close();
}
double compute_standard_deviation(vector<double> numbers)
{
double sum = 0, average = 0;
for (unsignedint i = 0; i < numbers.size(); i++)
{
sum = sum + numbers[i];
}
average = sum / numbers.size();
sum = 0;
for (unsignedint i = 0; i < numbers.size(); i++)
{
sum += pow(numbers[i] - average, 2);
}
return sqrt(sum / numbers.size());
}