luc062.c (1355B)
1 /* 2 * Author : Amit Dutta <amitdutta4255@gmail.com> 3 * Date : 08 Feb 2026 4 * Repo : https://github.com/notamitgamer/bsc 5 * License : MIT License (See the LICENSE file for details) 6 */ 7 8 /* For the following set of n data points (x, y), write a program to compute the correlation coefficient r. 9 */ 10 11 /* Let Us C, Chap- 13 (Arrays), Qn No.: B(h) */ 12 13 /* This file is auto-generated by a bot. */ 14 /* This code is not compiled; it is for reference only. */ 15 16 17 #include <stdio.h> 18 #include <math.h> 19 #include <stdlib.h> 20 21 int main() 22 { 23 double x[] = {34.22, 39.87, 41.85, 43.23, 40.06, 53.29, 53.29, 54.14, 49.12, 40.71, 55.15}; 24 double y[] = {102.43, 100.93, 97.43, 97.81, 98.32, 98.32, 100.07, 97.08, 91.59, 94.85, 94.65}; 25 26 int n = 11, i; 27 double sum_x = 0, sum_y = 0, sum_xy = 0, sum_x2 = 0, sum_y2 = 0; 28 double numerator, denominator, r; 29 30 for (i = 0; i < n; i++) 31 { 32 sum_x += x[i]; 33 sum_y += y[i]; 34 sum_xy += x[i] * y[i]; 35 sum_x2 += x[i] * x[i]; 36 sum_y2 += y[i] * y[i]; 37 } 38 39 numerator = (n * sum_xy) - (sum_x * sum_y); 40 denominator = sqrt((n * sum_x2 - sum_x * sum_x) * (n * sum_y2 - sum_y * sum_y)); 41 42 if (denominator != 0) 43 r = numerator / denominator; 44 else 45 r = 0; // Avoid division by zero 46 47 printf("Correlation coefficient (r) = %.4f\n", r); 48 49 return 0; 50 }