

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to find correlation coefficient in C++
In this tutorial, we will be discussing a program to find correlation coefficient.
For this we will be provided with two arrays. Our task is to find the correlation coefficient denoting the strength of the relation between the given values.
Example
#include<bits/stdc++.h> using namespace std; //function returning correlation coefficient float find_coefficient(int X[], int Y[], int n){ int sum_X = 0, sum_Y = 0, sum_XY = 0; int squareSum_X = 0, squareSum_Y = 0; for (int i = 0; i < n; i++){ sum_X = sum_X + X[i]; sum_Y = sum_Y + Y[i]; sum_XY = sum_XY + X[i] * Y[i]; squareSum_X = squareSum_X + X[i] * X[i]; squareSum_Y = squareSum_Y + Y[i] * Y[i]; } float corr = (float)(n * sum_XY - sum_X * sum_Y) / sqrt((n * squareSum_X - sum_X * sum_X) * (n * squareSum_Y - sum_Y * sum_Y)); return corr; } int main(){ int X[] = {15, 18, 21, 24, 27}; int Y[] = {25, 25, 27, 31, 32}; int n = sizeof(X)/sizeof(X[0]); cout<<find_coefficient(X, Y, n); return 0; }
Output
0.953463
- Related Questions & Answers
- How to find p-value for correlation coefficient in R?
- How to find the group-wise correlation coefficient in R?
- How to extract correlation coefficient value from correlation test in R?
- How to find the correlation coefficient between two data frames in R?
- How to find the correlation coefficient between rows of two data frames in R?
- How to convert a correlation matrix into a logical matrix based on correlation coefficient in R?
- How to change the size of correlation coefficient value in correlation matrix plot using corrplot in R?
- Find the combination of columns for correlation coefficient greater than a certain value in R
- How to find the significant correlation in an R data frame?
- C++ Program for Coefficient of variation
- How to round correlation values in the correlation matrix to zero decimal places in R?
- Exploring Correlation in Python
- How to find the correlation between corresponding columns of two matrices in R?
- How to find the groupwise correlation matrix for an R data frame?
- Correlation and Regression in Python
Advertisements