AGH Computer Science C Programming Laboratory 7
AGH Computer Science C Programming Laboratory 7
2024
Linear regression attempts to model the relationship between two variables by fitting a linear
equation to observed data. A linear regression line has an equation of the form Y = a*X+b, where X
is the explanatory variable and Y is the dependent variable. The slope of the line is a and b is the
intercept (the value of y when x = 0).
A) Write a C program that calculates the linear regression coefficients. Write one C program in
which all the following functions will be defined and called.
1
Point 14 = (90.0, 131.9)
Point 15 = (95.0, 134.1)
b) [4points] Write a function that will return the average value of the array passed.
In the main function:
- call the average function for the array x.
- call the average function for the array y.
Pass to the function: the array and its size.
The most common type of average is the arithmetic mean. If n numbers are given, each number
denoted by a[i] (where i = 1, 2, ..., n), the arithmetic mean is the sum of the as divided by n
∑𝑛
or𝐴𝑣𝑔𝑎[] = 𝑖=0𝑛 𝑎[𝑖].
c) [3points] Write a function that calculates d according to the formula𝑑 = ∑𝑛𝑖=0(𝑥[𝑖] − 𝑎𝑣𝑔𝑋) ∗ (𝑥[𝑖] −
𝑎𝑣𝑔𝑋), where x[i] are the elements of the array, avgX is the average value of the x array calculated in
the previous step.
In the main function, call the function calculating d for the array x. Pass to the function: the array,
its size, and the average value of the array calculated in the previous step.
e) [2points] Write a function that calculates b according to the formula𝑏 = 𝑎𝑣𝑔𝑌 − 𝑎 ∗ 𝑎𝑣𝑔𝑋 , where
avgY is the average value of the y array calculated in the previous steps, avgX is the average value of
the x array calculated in the previous steps, a is the value calculated in the previous step.
2
Print the result.
DeltaY = 0.43
In the main function, calculate DeltaA and DeltaB according to the following formulas:𝐷𝑒𝑙𝑡𝑎𝐴 =
𝐷𝑒𝑙𝑡𝑎𝑌 1 𝑎𝑣𝑔𝑋 2
, 𝐷𝑒𝑙𝑡𝑎𝐵 = 𝐷𝑒𝑙𝑡𝑎𝑌 ∗ √𝑛 + .
√𝑑 𝑑
DeltaA and DeltaB determine the number of significant digits of a and b, respectively.
B. [3points] Using the example from lecture 5, split the program into 3 files (e.g. reg.c, main.c,
reg.h) and create a makefile.
Next time:
laboratory 08 – Recursive functions
To prepare for the next class, read the lecture or book chapter on recursive functions.
Check how sample programs from the lecture or book work. Check if you can modify them
in any way you want. Write some example programs that use recursive functions.