0% found this document useful (0 votes)
15 views

AGH Computer Science C Programming Laboratory 7

Uploaded by

michalgach.0ff
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

AGH Computer Science C Programming Laboratory 7

Uploaded by

michalgach.0ff
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Laboratory 07 – Functions 20.11.

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.

a) [2points] In the main function create two 15-element arrays of floats.


In the first array x, the first element is equal to 25, each subsequent element is 5 larger than the
previous one. Write a loop to fill the array x.
In the second array y we have the following values: {109.4, 110.1, 112.0, 114.7, 116.0, 118.1,
119.5, 121.8, 123.1, 124.9, 127.6, 129.4, 130.6, 131.9, 134.1}.
Use the #define directive in the program.
Print the arrays in the following form:
Point 1 = (25.0, 109.4)
Point 2 = (30.0, 110.1)
Point 3 = (35.0, 112.0)
Point 4 = (40.0, 114.7)
Point 5 = (45.0, 116.0)
Point 6 = (50.0, 118.1)
Point 7 = (55.0, 119.5)
Point 8 = (60.0, 121.8)
Point 9 = (65.0, 123.1)
Point 10 = (70.0, 124.9)
Point 11 = (75.0, 127.6)
Point 12 = (80.0, 129.4)
Point 13 = (85.0, 130.6)

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𝑛 𝑎[𝑖].

Print the results.


average of x[] = 60.00
average of y[] = 121.55

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.

Print the result.


d of x[] = 7000.00
∑𝑛
d) [3points] Write a function that calculates a according to the formula𝑎 = 𝑖=0 𝑦[𝑖]∗(𝑥[𝑖]−𝑎𝑣𝑔𝑋)
𝑑
, where
x[i] and y[i] are the elements of the arrays, avgX is the average value of the x array calculated in the
previous steps, and d is the value calculated in the previous step.

In the main function, call the function calculating a.

Print the result.


a = 0.362

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.

In the main function, call the function calculating b.

Print the result.


b = 99.8

f) [3points] Write a function that calculates DeltaY according to the formula𝐷𝑒𝑙𝑡𝑎𝑌 =


∑𝑛 (𝑦[𝑖]−(𝑎∗𝑥[𝑖]+𝑏))2
√ 𝑖=0
𝑛−2
, where x[i] and y[i] are the elements of the arrays, a,b are the value calculated in
the previous steps, n is the size of the array.

In the main function, call the function calculating DeltaY.

2
Print the result.
DeltaY = 0.43

In the main function, calculate DeltaA and DeltaB according to the following formulas:𝐷𝑒𝑙𝑡𝑎𝐴 =
𝐷𝑒𝑙𝑡𝑎𝑌 1 𝑎𝑣𝑔𝑋 2
, 𝐷𝑒𝑙𝑡𝑎𝐵 = 𝐷𝑒𝑙𝑡𝑎𝑌 ∗ √𝑛 + .
√𝑑 𝑑

Print the result.


DeltaA = 0.005, DeltaB = 0.324

DeltaA and DeltaB determine the number of significant digits of a and b, respectively.

a has 3 significant digits and b has one significant digit.


a = 0.362
b = 99.8
Linear regression line has the equation Y = 0.362 * X + 99.8

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.

You might also like