<\/span><\/h2>\n\n\n\nIn this challenge, we practice calculating the Pearson correlation coefficient<\/em>.<\/p>\n\n\n\n<\/span>Task<\/strong><\/span><\/h2>\n\n\n\nGiven two n<\/em><\/strong>-element data sets, X<\/em><\/strong> and Y<\/em><\/strong>, calculate the value of the Pearson correlation coefficient.<\/p>\n\n\n\n<\/span>Input Format<\/strong><\/span><\/h2>\n\n\n\nThe first line contains an integer, n<\/em><\/strong>, denoting the size of data sets X<\/em><\/strong> and Y<\/em><\/strong>.
The second line contains n<\/em><\/strong> space-separated real numbers (scaled to at most<\/em> one decimal place), defining data set X<\/em><\/strong>.
The third line contains n<\/em><\/strong> space-separated real numbers (scaled to at most<\/em> one decimal place), defining data set Y<\/em><\/strong>.<\/p>\n\n\n\n<\/span>Constraints<\/strong><\/span><\/h2>\n\n\n\n- 10 <= n<\/em> <= 100<\/strong><\/li>
- 1 <= xi<\/sub><\/em> <= 500<\/strong>, where xi<\/sub><\/em><\/strong> is the ith<\/sup><\/em><\/strong> value of data set X<\/em><\/strong>.<\/li>
- 1 <= yi<\/sub><\/em> <= 500<\/strong>, where yi<\/sub><\/em><\/strong> is the ith<\/sup><\/em><\/strong> value of data set Y<\/em><\/strong>.<\/li>
- Data set X<\/em><\/strong> contains unique values.<\/li>
- Data set Y<\/em><\/strong> contains unique values.<\/li><\/ul>\n\n\n\n
<\/span>Output Format<\/strong><\/span><\/h2>\n\n\n\nPrint the value of the Pearson correlation coefficient, rounded to a scale of 3<\/strong> decimal places.<\/p>\n\n\n\nSample Input<\/strong><\/p>\n\n\n\n10\n10 9.8 8 7.8 7.7 7 6 5 4 2 \n200 44 32 24 22 17 15 12 8 4<\/code><\/pre>\n\n\n\nSample Output<\/strong><\/p>\n\n\n\n0.612<\/code><\/pre>\n\n\n\n<\/span>Solution – Pearson Correlation Coefficient I<\/strong><\/span><\/h2>\n\n\n\n<\/span>C++<\/strong><\/span><\/h3>\n\n\n\n#include <cmath>\n#include <cstdio>\n#include <vector>\n#include <iostream>\n#include <algorithm>\nusing namespace std;\n\ndouble pearson(double x[], double y[], int n, double mean1, double std1, double mean2, double std2)\n{\n double num = 0;\n for(auto i=0; i<n; i++)\n {\n num += ((x[i]-mean1)*(y[i]-mean2));\n }\n double den = (n*std1*std2);\n \n double res = num\/den;\n \n return res; \n}\n\nint main() {\n \/* Enter your code here. Read input from STDIN. Print output to STDOUT *\/ \n \n int n; cin>>n; double x[n+4], y[n+4]; double sum1=0, sum2=0, sum3=0, sum4=0;\n \n for(auto i=0; i<n; i++)\n {\n cin>>x[i];\n sum1 += x[i];\n }\n double mean1 = (double)(sum1\/n); \n for(auto i=0; i<n; i++)\n {\n sum2 += (((double)x[i]-mean1)*((double)x[i]-mean1));\n } \n double std1 = (double)sqrt(sum2\/n);\n \n for(auto i=0; i<n; i++)\n {\n cin>>y[i];\n sum3 += y[i];\n }\n double mean2 = (double)(sum3\/n); \n for(auto i=0; i<n; i++)\n {\n sum4 += (((double)y[i]-mean2)*((double)y[i]-mean2));\n } \n double std2 = (double)sqrt(sum4\/n);\n \n double pearson_coeff = pearson(x, y, n, mean1, std1, mean2, std2);\n \n printf(\"%0.3f\", pearson_coeff);\n\n \n return 0;\n}<\/pre>\n\n\n\n