In this problem, you’ll have a file called data.txt containing pairs:
x y
24 47
31 40
21 51
30 66
.. ..
You will write two functions:
slope: a function to calculate the slope of the linear regression lineintercept: a function to calculate the y-intercept of the linear regression line
Relevant Formulas
For a linear regression line in the form
- The slope can be computed using the formula:
- The intercept can be found using the formula:
Where is the number of pairs in our regression calculation.
Recall that represents summation notation:
Helper Functions
You may find it helpful to write a function sum that returns the sum of some array in writing your slope and intercept functions.
Main Function
In your main function, you should call slope and intercept and print their outputs to the terminal.
Arrays
Recall that in the C language, we must keep track of how many items are in our array. Can we read the file and store the contents as a two-dimensional array? As two one-dimensional arrays?
Hint:
int pairs[][2] = {{x0, y0}, {x1, y1}, {x2, y2}, ..., {xn, yn}};
int xVals[] = {x0, x1, x2, ... xn};
Sample Output
slope | intercept |
|---|---|
2.04 | 10.213 |
| This represents a linear equation in the form . |