standard deviation

okay.... I am completely new at this and am stuck. Here is my problem that I am trying to achieve:
The standard deviation, S, of a list of N numbers x{subi}is defined as
follows:
(it is sumthin like this, just with math symbols....)
s = sqrt ( { sum (x^2) - [sum (x)]^2 / n } / (n - 1) )


x{sub1}, x{sub2}...., Define a function that takse a partially filled
array of numbers as its arguments and returns the standard deviation of
the numbers in the partially fille array. Since a partially filled array
requires two arguments, the function will automatically have two formal
parameters: An array parameter and a formal parameter of type int that
gives the number of array positions used. The numbers in the array will be
of type double. Embeded your function in a suitable test program.

** And so far... this is where i am....

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <fstream>
#include <iostream>
using namespace std;

float average (float vector[], int size);
float deviation( &inputfile);

int main ()
{
	ifstream inputfile;
	float N;
	int size = 0;
	const int Num_List = 1000;	

	int List[Num_List];

	inputfile.open("program8.txt");
	if(inputfile.fail())
	{
		cout << "Error: could not open program8.txt" << endl;
		exit (EXIT_FAILURE);
	}

	for (int count=0; count < Num_List; count++)
		inputfile >> Num_List[count];



	float standDeviation [1000];

	inputfile >> N;
	while(!inputfile.eof())
	{
		standDeviation[size] = N;
		standDeviation [size]++;
		inputfile >> N;
	}

	average(standDeviation[], size);
	
 
	cout << "Standard Deviation : " << deviation(inputfile) << endl;

	inputfile.close();
	system("Pause");
	return 0;
}



//-----------------------------------------------------------------------------
// average funciton
//-----------------------------------------------------------------------------

float average (float vector[], int size)
{
	float sum, average;
	sum = 0.0;
	for (int i = 0; i < size; i++)
		sum += vector[size];

	if (size == 0)
		average = 0.0;
	else
		average = sum / size;

	return average;
}

//-----------------------------------------------------------------------------
// deviation function
//-----------------------------------------------------------------------------

float deviation( &inputfile)
{
	int N;
	double S;
	
	inputfile >> N;
		
	while(!sum == 0)
	{
		S = sqrt ((( sum (N ceil(2.0)) - (average ceil(2.0))) / (n - 1);
		inputfile >> N;
	}


	return S;
}
There appear to be two parts to this problem - firstly to load the list of doubles from the file 'program8.txt', the second to calcualte the SD for the list.

A good idea when learning (and later, for that matter) it to break the problem down like this and test each part of the solution as you go - it reduces the headache when trying to figure out whats wrong (as you test and fix each section as you go, so you always only have a small problem to worry about:-)

Your code suggests you have a fixed number of doubles to load (Num_List = 1000), if so, a couple of changes will fix it for this.
1
2
3
4
5
double List[Num_List];   //Array of doubles, not int
...
for (int count=0; count < Num_List; count++)
  inputfile >> List[count];     //input to array, not constant
inputfile.close();             //All done with the inputfile, so close. 


You can then add a simple loop to check this works
1
2
for (int count=0; count < Num_List; count++)
  cout >> "\t" >> List[count];


If you do not have a fixed number of doubles to load this appraoch will not work (obviously), and instead you will have to use a dynamic array.

Once you have this working (or have problmes doing so, let us know and we can help further.
Okay, that helped me. Now i just need to a simple way to find the sum of my input file numbers, using a funciton. I know it is probably simple, I just can't seem to be able to figure it out.

If you look at the function 'average' it actulay does that.
Just remove the float variable 'average', the if..else section and return 'sum'.
You probably want to rename the mofidied function as 'sum' to make the use clear as well:-)
Making progress!!! I actually got it to run, i am just getting this wierd output. it says... Standard Deviation : -1.#IO. I don't know why i am getting this. Here is my code:
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115

#include <fstream>
#include <iostream>
using namespace std;

float average (float vector[], int size);
float deviation( float sum, float average,float N );
float sum (float vector[], int size);

int main ()
{
	ifstream inputfile;
	float N;
	int size = 0;
	const int Num_List = 1000;
	float summ, avg;

	double List[Num_List];

	inputfile.open("program8.txt");
	if(inputfile.fail())
	{
		cout << "Error: could not open program8.txt" << endl;
		exit (EXIT_FAILURE);
	}

	for (int count=0; count < Num_List; count++)
		inputfile >> List[count];



	float standDeviation [100];

	inputfile >> N;
	while(!inputfile.eof())
	{
		standDeviation[size] = N;
		size++;
		inputfile >> N;
	}

	summ = sum (standDeviation, size);
	avg = average(standDeviation, size);
	
	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(3);
 
	cout << "Standard Deviation : " << deviation(summ, avg, size) << endl;

	inputfile.close();
	cin.get();
	cin.get();
	return 0;
}

//-----------------------------------------------------------------------------
// sum function
//-----------------------------------------------------------------------------

float sum (float vector[], int size)
{
	float sum;
	sum = 0.0;
	for (int N = 0; N < size; N++)
	{
		sum +=vector[N] ;
	}


	return sum;
}

//-----------------------------------------------------------------------------
// average function
//-----------------------------------------------------------------------------

float average (float vector[], int size)
{
	float sum, average;
	sum = 0.0;
	for (int i = 0; i < size; i++)
		sum += vector[size];

	if (size == 0)
		average = 0.0;
	else
		average = sum / size;

	return average;
}


//-----------------------------------------------------------------------------
// deviation function
//-----------------------------------------------------------------------------
#include <cmath>
float deviation(float sum, float average, float N)
{
	double S;
	double U;
	
	
	U =  ( sum -   average ) * ceil (2);
	S = (sqrt (U / N));


	return S;
}





Last edited on
Anyone know how to solve my problem?
closed account (z05DSL3A)
Just a scan over your code:

1
2
3
4
5
6
7
8
9
10
11
12
float deviation(float sum, float average, float N)
{
	double S;
	double U;
	
	
	U =  ( sum -   average ) * ceil (2);
	S = (sqrt (U / N));


	return S;
}


You are returning a double through float, you are passing an int in through 'float N', and I suspect that there would be a warning about ambiguity in 'ceil (2)'
Last edited on
Well, the reason you are getting 1.#IO is the call to deviation
 
     cout << "Standard Deviation : " << deviation(summ, avg, size) << endl;

I believe that size will be zero, and hence.
 
	S = (sqrt (U / N));

involves division by zero.

Looking at your code it appears that you are trying to count the number of elements in the loop
1
2
3
4
5
6
7
    inputfile >> N;
    while(!inputfile.eof())
    {
        standDeviation[size] = N;
        size++;
        inputfile >> N;
    }

This fails because the previous loop to input into List will have caused inputfile.eof() to become true, hence this loop is never executed, and size remains zero.

The check for eof() and need to count the number of elements suggests you do not have a fixed number of elements, and so should be using dynamic memory allocation, but assuming you know it will always be 1000 or less you can stick with the simple static arrays and a counter (it's just not very efficient).

I woudl replace
1
2
3
4
5
6
7
8
9
10
11
12
13
14
    double List[Num_List];
    ....
    for (int count=0; count < Num_List; count++)
        inputfile >> List[count];

    float standDeviation [100];

    inputfile >> N;
    while(!inputfile.eof())
    {
        standDeviation[size] = N;
        size++;
        inputfile >> N;
    }

by
1
2
3
4
5
6
7
8
9
    double standDeviation [Num_List];       

    inputfile >> N;
    while((!inputfile.eof()) && (size<Num_List))
    {
        standDeviation[size] = N;
        size++;
        inputfile >> N;
    }

This will load the array standDeviation and set size. The additional check in the while condition is to avoid overrunning the array bounds.

Note that I have declared standDeviation as double - it probably makes sense to standardise on double throughout (IE replace all float by double).

I woudl also avoid using varaibles inside a function with the same name as the function (or any other functions). The compiler should be able to cope, but the code is less obvious.
1
2
3
4
5
6
7
8
9
10
11
float sum (float vector[], int size)
{
	float theSum;
	theSum = 0.0;
	for (int N = 0; N < size; N++)
	{
		theSum +=vector[N] ;
	}

	return theSum;
}

dittio for average.

Finally, you are using ceil {round up} when you probably want pow {raise to power of} in deviation, and you have also changed the calculation in deviation - is this what you intended?
I believe that i have found my problem. Now, i just need to find a way to fix it. It is in my average funciton.

Can someone look at my average function and help me see what i am doing wrong?
Last edited on
Topic archived. No new replies allowed.