luc062.c ​
Metadata ​
- Author — Amit Dutta
- Last updated — 08 Feb 2026
- License — MIT License (See the LICENSE file for details)
Problem Statement ​
Problem Statement
For the following set of n data points (x, y), write a program to compute the correlation coefficient r.
Source Code ​
Printing the code
To print this file, open it on GitHub and click Raw before printing, or use the Download Raw button above and print directly from that page.
c
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main()
{
double x[] = {34.22, 39.87, 41.85, 43.23, 40.06, 53.29, 53.29, 54.14, 49.12, 40.71, 55.15};
double y[] = {102.43, 100.93, 97.43, 97.81, 98.32, 98.32, 100.07, 97.08, 91.59, 94.85, 94.65};
int n = 11, i;
double sum_x = 0, sum_y = 0, sum_xy = 0, sum_x2 = 0, sum_y2 = 0;
double numerator, denominator, r;
for (i = 0; i < n; i++)
{
sum_x += x[i];
sum_y += y[i];
sum_xy += x[i] * y[i];
sum_x2 += x[i] * x[i];
sum_y2 += y[i] * y[i];
}
numerator = (n * sum_xy) - (sum_x * sum_y);
denominator = sqrt((n * sum_x2 - sum_x * sum_x) * (n * sum_y2 - sum_y * sum_y));
if (denominator != 0)
r = numerator / denominator;
else
r = 0; // Avoid division by zero
printf("Correlation coefficient (r) = %.4f\n", r);
return 0;
}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
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