Using fstreams

Hi I'm having a huge problem with my code and I don't know what I'm doing wrong. I'm using fstreams to open a data file and using it to calculate some values but it's giving me wrong values. I'm using visual studio btw. I really don't know what I'm doing wrong


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

using namespace std;

int main()

{
    fstream in_stream;
    ofstream out_stream;
    in_stream.open("C:\\Users\\fatim\\Desktop\\Assignments SP 20\\data.txt");

    if (in_stream.fail())
    {
        cout << "Input file opening failed \n";
        exit(1);
    }
    int n = 21, i = 0;
    float x[21], y[21];
    for (int i = 0; i <= n; i++);
    {
        in_stream >> x[i] >> y[i];
    }

    in_stream.close();

    /* out_stream.open("C:\\Users\\fatim\\Desktop\\Assignments SP 20\\data out.txt");
     if (out_stream.fail());
     {
         cout << "Output file opening failed \n";
     }
     */

    double sumx = 0, sumxy = 0, sumy = 0, sumx2 = 0, st = 0, sr = 0, sr2 = 0;
    for (int i = 0; i < n; i++);
    {
        sumx += sumx + x[i];
        sumy += sumy + y[i];
        sumxy += sumxy + x[i] * y[i];
        sumx2 += sumx2 + x[i] * x[i];
    }

    double xm = 0, ym = 0;
    double a1, a0, a2;
    xm = sumx / n;
    ym = sumy / n;
    a1 = ((n * sumxy) - (sumx * sumy)) / ((n * sumx2) - (sumx * sumx));
    a0 = ym - (a1 * xm);
    a2 = sumxy / sumx2;

    for (int i = 0; i < n; i++)
    {
        st = (y[i] - ym) * (y[i] - ym);
        sr = (y[i] - (a1 * x[i]) - a0) * (y[i] - (a1 * x[i]) - a0);
        sr2 = (y[i] - a2) * (y[i] - a2);
    }
    double r2, r2new;
    r2 = (st - sr) / st;
    r2new = (st - sr2) / st;

    cout << "The coefficient of determination is " << r2 << endl;
    cout << "The y-intercept is " << a0 << endl;
    cout << "The coefficient of x is " << a1 << endl;
    cout << "The new coefficient of x is " << a2 << endl;
    cout << "The new coefficient of determination is " << r2new << endl;


    return 0;
}

in the loop in line 22 you have: i <= n
in the loops in line 37 ad 53 you have: i < n
so you do know that array indices go from 0 to n-1, ¿so why are you intentionally going out of bounds in your reading?
Hello lorenzamaryan,

In addition to what ne555 has said I see a couple of things.

if (in_stream.fail()). Using if (!in_stream) will do the same thing with the added advantage that it will check all of the state bits. If the "eof" bit is set it does not set the "fail" or "bad" bits.

Also do not use "exit()" use "return 1" instead since you are in "main".
jlb wrote:

You really should try to avoid the C function exit(), it doesn't know about C++ classes and can cause data loss if used inappropriately. Since this is C++ start thinking about using exceptions when you need to "abort" the program.



I also noticed that most people tend to write sumx = sumx + x[i];. Lets say the "sum" = 3 and "x[i]" = 1 with the result expected being 4. The first code would do this. What you have written sumx += sumx + x[i]; would be 3 + 1 = 4 then add this to "sum",(3) which gives you a result of 7. All you need here is sumx += x[i];. And the same is true for the other 3 lines in the for loop.

Try that and see if it does not work better.

Andy

Edit:

If you include your input file, or a good sample, everyone can be using the same information to work with.
Last edited on
Topic archived. No new replies allowed.