std dev.

Need a little help.

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:
Need a little help.

Help on what, exactly?
Unsure how to do this part...

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.
You mean how to implement the
void read_numbers(ifstream& f, vector<double>& numbers);

What if you would not have stream f, but would have to read from std::cin into the vector. How would you do that?
basically this is what im starting with.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <fstream>
#include <vector>
using namespace 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;
}
So is this what all I need here?

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.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void read_numbers(ifstream& f, vector<double>& numbers)
{
	ifstream f;
	f.open("f1.txt");
	if (!f.is_open())
	{
		cout << "Couldn't open f1.txt." << endl;
		return -1;
	}
	double digits;
	while (f >> digits)
	{
		numbers.push_back(numbers);
	}

	f.close();
}
So is this what all I need here?

More than you need.

You wrote:
// 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?
Seems to be like I have an infinite loop.....

I bolded that part that is having issues and dont know what to do???

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <fstream>
#include <vector>
using namespace 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 (unsigned int i = 0; i < numbers.size(); i++)
	{
		sum = sum + numbers[i];
	}
	average = sum / numbers.size();
	sum = 0;
	for (unsigned int i = 0; i < numbers.size(); i++)
	{
		sum += pow(numbers[i] - average, 2);
	}
	return sqrt(sum / numbers.size());
}
Reread what the argc and argv contain. What if the user does not give command line parameters?

What is the purpose of line 16?
What is the purpose of line 20?

You already had earlier:
1
2
3
4
5
double digits;
while ( f >> digits )
{
  numbers.push_back( digits );
}

Why should the function do anything else?

What is the stdev, if numbers is empty?
Okay, so im not really sure why I put line 16 and 20. I was told to by someone else, but they dont make sense.

I reverted to this: and get -1IND when I debugg.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <fstream>
#include <vector>
using namespace 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 (unsigned int i = 0; i < numbers.size(); i++)
	{
		sum = sum + numbers[i];
	}
	average = sum / numbers.size();
	sum = 0;
	for (unsigned int i = 0; i < numbers.size(); i++)
	{
		sum += pow(numbers[i] - average, 2);
	}
	return sqrt(sum / numbers.size());
}
I believe I solved this. Thank you!
closed account (Diz60pDG)
I am writing a similar code, and getting the same error (-1IND) as you, what did you do to fix this? Where was the error coming from?
You say "similar". The devil is in the details, so show your exact code.
Topic archived. No new replies allowed.