Do a function by taking information from file

I have a coding to find standard deviation from the file provided. My lecturer suggest to do it in function. I managed to get rid of the error. However i get 0 for the answer. What should i do ? The new coding i have tried :

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
#include <iostream>
#include <cmath>
#include <fstream>
#include <string>
using namespace std;

float calculateSD(ifstream& nameFile);

int main()
{
	string input;  
	ifstream nameFile("data.txt"); 

	if (!nameFile)
		return (cout << " ERROR : cannot open file.\n"), 1; 

	while (getline(nameFile, input)) 
			cout << input << endl;

	nameFile.clear();   
	nameFile.seekg(0);  

    calculateSD(nameFile);
}

float calculateSD(ifstream& nameFile);
{   
    int a {}, b {}, c {}, d {}, e {};
    nameFile >> a >> b >> c >> d >> e;
    float sum1 = 0.0, sum2 = 0.0, sum3 = 0.0;
    float mean1, mean2, mean3;
    float standardDeviation1 = 0.0, standardDeviation2 = 0.0, standardDeviation3 = 0.0;

     for (int a {}, b {}, c {}, d {}, e {}; nameFile >> a >> b >> c >> d >> e; )
    {
        sum1 += c;
        sum2 += d;
        sum3 += e;
    }

    mean1 = sum1/10;
    mean2 = sum2/10;
    mean3 = sum3/10;

   for (int a {}, b {}, c {}, d {}, e {}; nameFile >> a >> b >> c >> d >> e; )
       {
            standardDeviation1 += pow(c - mean1, 2);
            standardDeviation2 += pow(d - mean2, 2);
            standardDeviation3 += pow(e - mean3, 2);
       }

     cout << "Standard Deviation 1  = " << sqrt(standardDeviation1 / 10) << endl;
     cout << "Standard Deviation 2 = " << sqrt(standardDeviation2 / 10)<< endl;
     cout << "Standard Deviation 3 = " << sqrt(standardDeviation3 / 10)<< endl;

    return 0;
}



Last edited on
case matters. line 46, nameFile is not namefile. you do this a few more times.
and its unclear what you are trying to do with the calculate sd function. it only takes 1 value, there is no SD really for 1 value. Did you mean to pass an array?
you can't pass filestream<<a<<b<<c to it, for sure. but I don't know what you wanted there.

I think, to start, you want...

float calcsd(float a, float b, float c)
{
... calculate and return value
}

... in main somewhere use it:
result = calcsd(a,b,c);
...
Last edited on
@jonnin the file have 3 column therefore i need to find standard deviation for each column. If i do result = calcsd(a,b,c) , will i get 3 result ?
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
#include <iostream>
#include <cmath>
#include <fstream>
#include <string>
using namespace std;

float calculateSD(float namefile istream& nameFile);

int main()
{
	string input;  
	ifstream namefile("payment.txt"); 

	if (!namefile)
		return (cout << " ERROR : cannot open file.\n"), 1; 

	while (getline(namefile, input)) 
			cout << input << endl;

	namefile.clear();   
	namefile.seekg(0);  

	int a {}, b {}, c {};
        nameFile >> a >> b >> c ;

    calculateSD(namefile nameFile >> a >> b >> c);
}

float calculateSD(float istream& nameFile)
{
    float sum1 = 0.0, sum2 = 0.0, sum3 = 0.0;
    float mean1, mean2, mean3;
    float standardDeviation1 = 0.0, standardDeviation2 = 0.0, standardDeviation3 = 0.0;

     for (int a {}, b {}, c {}; nameFile >> a >> b >> c; )
    {
        sum1 += a;
        sum2 += b;
        sum3 += c;
    }

    mean1 = sum1/10;
    mean2 = sum2/10;
    mean3 = sum3/10;

	nameFile.clear();   
	nameFile.seekg(0);  

   for (int a {}, b {}, c {}; nameFile >> a >> b >> c ; )
       {
            standardDeviation1 += pow(a - mean1, 2);
            standardDeviation2 += pow(b - mean2, 2);
            standardDeviation3 += pow(c - mean3, 2);
       }

     cout << "Standard Deviation 1  = " << sqrt(standardDeviation1 / 10) << endl;
     cout << "Standard Deviation 2 = " << sqrt(standardDeviation2 / 10)<< endl;
     cout << "Standard Deviation 3 = " << sqrt(standardDeviation3 / 10)<< endl;

    return 0;
}
Not tested!
no, I did not realize a,b,c were arrays.
your best bet is to read a,b,c into arrays and do them one by one. Do you know arrays, and allowed to use?
Line 24,26: spelling. namefile is lower case. You have an upper case F here. C++ is case sensitive.

Line 26: calculateSD is expecting a float. The expression nameFile >> a >> b >> c returns a bool. What do you think you're passing?

Line 35,46: namefile is not defined in your function. It is a local variable within main. You argument is called namefile. Were you attempting to pass the ifstream?
 
float calculateSD (ifstream & namefile);


Line 46: Your loop will fail on the first attempted read. You read the entire file at line 35.



Last edited on
Perhaps:

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
#include <iostream>
#include <cmath>
#include <fstream>
#include <string>

void calculateSD(std::istream& nameFile);

int main()
{
	std::ifstream namefile("payment.txt");

	if (!namefile)
		return (std::cout << " ERROR : cannot open file.\n"), 1;

	for (std::string input; std::getline(namefile, input); )
		std::cout << input << '\n';

	calculateSD(namefile);
}

void calculateSD(std::istream& nameFile)
{
	float sum1 {}, sum2 {}, sum3 {};
	size_t cnt {};

	nameFile.clear();
	nameFile.seekg(0);

	for (int a {}, b {}, c {}; nameFile >> a >> b >> c; ++cnt) {
		sum1 += a;
		sum2 += b;
		sum3 += c;
	}

	const auto mean1 {sum1 / cnt};
	const auto mean2 {sum2 / cnt};
	const auto mean3 {sum3 / cnt};

	float standardDeviation1 {}, standardDeviation2 {}, standardDeviation3 {};

	nameFile.clear();
	nameFile.seekg(0);

	for (int a {}, b {}, c {}; nameFile >> a >> b >> c; ) {
		standardDeviation1 += (a - mean1) * (a - mean1);
		standardDeviation2 += (b - mean2) * (b - mean2);
		standardDeviation3 += (c - mean3) * (c - mean3);
	}

	std::cout << "Standard Deviation 1 = " << std::sqrt(standardDeviation1 / cnt) << '\n';
	std::cout << "Standard Deviation 2 = " << std::sqrt(standardDeviation2 / cnt) << '\n';
	std::cout << "Standard Deviation 3 = " << std::sqrt(standardDeviation3 / cnt) << '\n';
}


but I seem to remember something about calculating sd without having to read the data twice? My statistics knowledge is limited to mean/median/mode!

Perhaps it was some @lastchance code??
Last edited on
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
#include <iostream>
#include <fstream>
#include <sstream>
#include <valarray>
#include <cmath>
using namespace std;


int main()
{
   const int COLS = 3;
// ifstream in( "data.txt" );
   istringstream in( "2456 128 234\n"
                     "2368 200 249\n"
                     "2655 212 250\n" );

   int N = 0;
   valarray<double> sum( 0.0, COLS ), sumsq( 0.0, COLS);
   for ( double a, b, c; in >> a >> b >> c; N++ )
   {
       valarray<double> V( { a, b, c } );
       sum += V;
       sumsq += V * V;
   }
   valarray<double> mean = sum / ( N + 0.0);
   valarray<double> std1 = sqrt( sumsq / ( N + 0.0 ) - mean * mean );   // standard deviation of the data
   valarray<double> std2 = std1 * sqrt( N / ( N - 1.0 ) );              // unbiased estimate of whole-population s.d.
   
   cout << "The column averages are: ";
   for ( double e : mean ) cout << e << " ";
   cout << "\n\n";
   cout << "The column standard deviations are: ";
   for ( double e : std1 ) cout << e << " ";                            // or use std2 if necessary
}


The column averages are: 2493 180 244.333

The column standard deviations are: 120.053 37.0945 7.31817 
Last edited on
Topic archived. No new replies allowed.