how to keep track of points without using an array?

i started doing a program that my professor assigned and realized that i can't use arrays because i dont know how many points there are going to be in the file that the user decides to open. i'm stuck on how to keep track and store all of the coordinates without an array.

here is the question:
2. You will write a program which calculates the “best fit” line for a number
of data points.
(a) Get the points from a user-specified file (the user should be able to
choose any filename). Each line of the file will contain an x-coordinate
and a y-coordinate, but you do not know how many lines are in the
file. (You will use: while(inFile.good()) for this purpose)
(b) Your program will then calculate the equation for the best-fit line
and display it to the monitor.

Because you do not know how many points the file will contain ahead of
time, you can’t use arrays in this problem (because you won’t know how
big to make them). Instead, you will need to keep track of the number of
points in the file, and all the necessary sums as you input each point.

here is what i have so far:

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

int main()
{
    ifstream inFile;
    
    cout << "Enter a file name containing the points: ";
    string inputFilename;
    cin >> inputFilename;
    
    inFile.open(inputFilename.c_str());
    
    int n, x, y;
    char p;
    while(inFile.good())
    {
        inFile >> n >> x >> p >> y;
    }
    
    system("PAUSE");
    return 0;
}


thanks!!!
This perplexed me at first, as well. My instructor assigned us something similar; as in, we did not know how many strings would be in the file. It sounds like you are going to have to dynamically calculate the line of best fit. This is not something I feel I can solve in any short period of time, but I want to at least get you thinking in the right direction. It sounds like you will have to first calculate line of best fit for the first two points in the file. Then add in the third point, and readjust the line of best fit, until you have read in all points (and recalculated line of best fit)...until the end of the file.
Last edited on
sounds more like a math exercise to me, but my old TI-59 programmable could do this using least squares:

http://hotmath.com/hotmath_help/topics/line-of-best-fit.html

If I remember correctly, it kept a running values of mean(x), mean(y), (current(x) - mean(x)) and (current(y) - mean(y)). Can't remember how it obtained mean of x and y, but I think it kept sums and count - not necessary, but makes those values available to the user, so it used 8 registers. If I remember you entered x, did an x transfer to t, entered y, then a function key to continue.

I don't know where my TI-59 went, but sometimes I miss it . . . Actually I do still have some printouts of the program codes for some of the math library programs, but they are pretty faded and hard to read.
Last edited on
And most of you haven't got the faintest idea what a TI-59 was!
no google, please! :)
Last edited on
here is a program that runs but it is not calculating the correct best fit line, i think the problem is somewhere in the while loop because i don't know if it is performing each statement after each point is being read from the file

here is my program:
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<cstdlib>
#include<fstream>
#include<cmath>
#include<iomanip>
using namespace std;

int main()
{
    ifstream inFile;
    
    cout << "Enter a file name containing the points: ";
    string inputFilename;
    cin >> inputFilename;
    
    inFile.open(inputFilename.c_str());
    
    double sumx=0;
    double sumy=0;
    double sumxs=0;
    double sumys=0;
    double sumxy=0;
    int n=0;
    
    double x, y;
    int c;
    char p;
    while(inFile.good())
    {
        inFile >> c >> x >> p >> y;
        sumx+=x; //to find the sum of x values
        sumy+=y; //to find the sum of y values
        sumxs+=pow(x,2); //to find the sum of x squared values
        sumys+=pow(y,2); //to find the sum of y sqaured values
        sumxy+=(x*y); //to find the sum of product of x and y values
        n++; //to keep track of how many points there are to find averages
    }
    
    double avgx=sumx/n; //to find the average of x values
    double avgy=sumy/n; //to find the average of y values
    double avgxs=sumxs/n; //to find the average of x squared values
    double avgys=sumys/n; //to find the average of y squared values
    double avgxy=sumxy/n; //to find the average of product x and y values
    
    double a=(avgxy-avgx*avgy)/(avgxs-pow(avgx,2)); //to calculate a
    double b=avgy-(a*avgx); //to calculate b
    
    cout << "The best fit line is given by y="
    << a << "x" << showpos << b <<endl; //y=ax+b
    
    system("PAUSE");
    return 0;
}
Last edited on
Looks like there are a few issues in line 45 - double check your source to verify you're using the correct values. I think you may be using some averages where sums are needed.
Topic archived. No new replies allowed.