Calc standard deviation

I need to calculate the mean and standard deviation of 16 house prices. The data is being read in from a text file. The program compiles, but it does not correctly calculate the mean and standard deviation. Could this be a read in mishap or is something wrong in my calculation? Please help

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

void instruct();

void readin(double num[], int& n);

double calc(double num[]);

int main()
{

//declarations
 int n;
 double num[100], mean, stdDev;

//input
instruct();

readin(num, n);

//processing
calc(num);

//output
cout << "Standard Deviation: " << stdDev << endl;
cout << "Mean: " << mean << endl;
}

 void instruct()
{
cout << "This program calculates the mean and standard deviation of house prices. " << endl;
}

void readin(double num[], int& n)
{
  ifstream infile;
  infile.open("prices.txt");
  n = 0;
  while (infile >> num[n]) n++;
}

double calc(double num[])
{
 double sum = 0.0, mean = 0.0, stdDev = 0.0;
 int i;

  for(i = 0; i < 16; i++)
  {
    sum += num[i];
  }

  mean = sum/16;

  return mean;

  for(i = 0; i < 16; i++)
  {
    stdDev += pow(num[i] - mean, 2);
  }

  return sqrt(stdDev / 16);
}


First, check if your infile is opening successfully:
1
2
3
4
5
ifstream infile("prices.txt"); // line 39
if (! infile)
{
    cout << "Error: Could not open prices.txt!\n";
}


Second, a function can only return one thing.

Your calc function ends on line 57, after you return mean. The code beyond it is dead code (unreachable).
Last edited on
I made sure the file was opening properly and it was. I also made 2 separate functions to calculate the mean and standard deviation but I'm still getting weird numbers for my mean and standard deviation. When I run it I get:
This program calculates the mean and standard deviation of house prices.
Standard Deviation: 6.94649e-310
Mean: 6.94649e-310


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

void instruct();

void readin(double num[], int& n);

double calcMean(double num[]);

double calcDev(double num[], double mean);

int main()
{

//declarations
 int n;
 double num[100], mean, stdDev;

//input
instruct();

readin(num, n);

//processing
calcMean(num);

calcDev(num, mean);
//output
cout << "Standard Deviation: " << stdDev << endl;
cout << "Mean: " << mean << endl;
}

void instruct()
{
cout << "This program calculates the mean and standard deviation of house prices. " << endl;
}

void readin(double num[], int& n)
{
  ifstream infile;
  infile.open("prices.txt");
  n = 0;
  while (infile >> num[n]) n++;
}
double calcMean(double num[])
{
 double sum = 0.0, mean = 0.0;
 int i;

  for(i = 0; i < 16; i++)
  {
    sum += num[i];
  }

  mean = sum/16;

  return mean;
}

double calcDev(double num[], double mean)
{
int i;
double stdDev = 0.0;
  for(i = 0; i < 16; i++)
  {
    stdDev += pow(num[i] - mean, 2);
  }

  return sqrt(stdDev / 16);
}

You are never using the return value of your calcMean function.
The mean variable in main is never set to anything.

mean = calcMean(num);

Same thing for stdDev. You are never assigning a proper value to it. You should turn on compiler warnings in your IDE or compiler, because they would flag things like this as "uninitialized variable" warnings.

stdDev = calcDev(num, mean);

Also, you can declare and initialize variables on the same line.

So, for example, instead of doing
1
2
3
4
5
double mean;

// ...

mean = calcMean(num);


You can just do:
1
2

double mean = calcMean(num);


And i can be declared inside the for loop.
1
2
for (int i = 0; i < 16; i++)
    ... 
Last edited on
Topic archived. No new replies allowed.