You are on page 1of 2

Programming Fundamentals (COMP 111)

Assignment# 02 CLO# 02
Due Date: November 30, 2022 Max Marks: 10

The formulae to draw a regression line to best fit a given set of data points {(x1, y1),
(x2, y2), (x3, y3), ……., (xn, yn)} are provided as following:

n
S x =∑ x i (1)
i=1

n
S y =∑ y i (2)
i=1

n
S xx=∑ x i
2
(3)
i=1

n
S xy =∑ x i yi (4)
i=1

Sx
x= (5)
n
Sy
y= (6)
n
n ∙ S xy−S x ∙ S y
m= (7)
n ∙ S xx−S x ∙ S y

b= y−m∙ x (8)

Then the equation for the best fitted regression line is,

y=m∙ x+b (9)

In Equation 1-9, x i and y i are ith x and y coordinates of a point, n is the number of data
points, m is slope and b is the y-intercept.

Write a complete C++ program that asks users to input number of data points they
want to fit with regression line and then input coordinates of points (x and y
coordinates) i.e., x i and y i. It should then implement these formulae to obtain and print
equation of the regression line.
Finally, it should ask the user to input x values for which it will use Equation (9) to
compute the corresponding predicted y values.

See below, a sample output of the program for 5 data points to validate your code.

Hints: Use for loop to calculate summations in Equation (1-4), flags


setprecision() and setw() for formatting output.

You might also like