Program output issue

I'm writing code for a project of mine. I have to load data in through a file, and run four different functions to calculate and output the largest value, smallest value, mean, and standard deviation. Problem is, the output for all four values when I run the program is 0. Code below, whats the issue?

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

void readIn(int& n, double data[])
{
        ifstream fin;
        fin.open("data.txt.");
        int i=0;

        while (fin >> data[i])
        {
                fin >> data[i];
                i++;
        }

        n = i + 1;
        fin.close();
}

double calcMean(int n, double data[])
{
        double sum, mean;
        int i;
        sum = 0;
        for(i=0; i<n; i++)
        {
                sum = sum + data[i];
        }
        mean = sum / n;
        return mean;
}

double calcDev(int n, double data[], double mean)
{
        int i;
        double stDev;
        stDev = 0;
        for(i=0; i<n; i++)
        {
                stDev = stDev + (data[i] - mean)*(data[i] - mean);
        }
        return stDev;
}

double standardDev(int n, double stDev)
{
        double stdDev;
        stdDev = sqrt(stDev / n);
        return stdDev;
}

double calcLargest(int n, double data[])
{
        int i;
        double X;
        X = data[0];
        for(i=1; i<=n; i++)
        {
                if( X < data[i])
                X = data[i];
        }
        return X;
}

double calcSmallest(int n, double data[])
{
        int i;
        double Y;
        Y = data[0];
        for(i=1; i <=n; i++)
        {
                if (Y > data[i])
                Y = data[i];
        }
        return Y;
}

int main()
{
        int n;
        double data[2000], mean, stDev, stdDev, largest, smallest;

        readIn(n, data);
        mean = calcMean(n, data);
        stDev = calcDev(n, data, mean);
        stdDev = standardDev(n, stDev);
        largest = calcLargest(n, data);
        smallest = calcSmallest(n, data);

        cout << "The largest value is " << largest << ".\n";
        cout << "The smallest value is " << smallest << ".\n";
        cout << "The mean value of these data is " << mean << ".\n";
        cout << "The standard deviation of these data is " << stdDev << ".\n";

}
Have you verified that your data is being read in correctly?

Do you realise that you're writing to data[i] twice during every iteration of your while loop?
Going back to iHutch105 is that you need to follow the data. As your program runs create little cout statements and have them outputting the answer to see where the problem is. Walk through your program by putting test values in on paper to see if it would be correct.

As I look back at your program usually text files return strings. There are plenty of resources out there so you can change strings to doubles. Everything else looks great though.
Topic archived. No new replies allowed.