How to find correlation coefficient?
Please check the below link to get better understanding on correlation coefficient.
Write a C program to calculate correlation coefficient.
Please check the below link to get better understanding on correlation coefficient.
Write a C program to calculate correlation coefficient.
#include <math.h>
int main() {
int x[100], y[100], xy[100], xsquare[100], ysquare[100];
int i, n, xsum, ysum, xysum, xsqr_sum, ysqr_sum;
float coeff, num, deno;
xsum = ysum = xysum = xsqr_sum = ysqr_sum = 0;
/* get the number of entries from the user */
printf("Enter the value for n:");
scanf("%d", &n);
/* get the values for x and y from the user */
printf("Enter the value for x and y:\n");
for (i = 0; i < n; i++) {
printf("x[%d] and y[%d]: ", i, i);
scanf("%d%d", &x[i], &y[i]);
}
/* find the needed data to manipulate correlation coeff */
for (i = 0; i < n; i++) {
xy[i] = x[i] * y[i];
xsquare[i] = x[i] * x[i];
ysquare[i] = y[i] * y[i];
xsum = xsum + x[i];
ysum = ysum + y[i];
xysum = xysum + xy[i];
xsqr_sum = xsqr_sum + xsquare[i];
ysqr_sum = ysqr_sum + ysquare[i];
}
num = 1.0 * ((n * xysum) - (xsum * ysum));
deno = 1.0 * ((n * xsqr_sum - xsum * xsum)* (n * ysqr_sum - ysum * ysum));
/* calculate correlation coefficient */
coeff = num / sqrt(deno);
/* print the result */
printf("Correlation Coefficient : %.4f\n", coeff);
return 0;
}
Note:
gcc corr_coeff.c -lm => linked math library since we have used sqrt() math function.
Output:
jp@jp-VirtualBox:~/$ gcc corr_coeff.c -lm
jp@jp-VirtualBox:~/$ ./a.out
Enter the value for n:6
Enter the value for x and y:
x[0] and y[0]: 43 99
x[1] and y[1]: 21 65
x[2] and y[2]: 25 79
x[3] and y[3]: 42 75
x[4] and y[4]: 57 87
x[5] and y[5]: 59 81
Correlation Coefficient : 0.5298
jp@jp-VirtualBox:~/$ ./a.out
Enter the value for n:6
Enter the value for x and y:
x[0] and y[0]: 43 99
x[1] and y[1]: 21 65
x[2] and y[2]: 25 79
x[3] and y[3]: 42 75
x[4] and y[4]: 57 87
x[5] and y[5]: 59 81
Correlation Coefficient : 0.5298
See Also:
- c program to print "Hello World"
- c program to print "Hello World" without using semicolon
- c program to swap two numbers
- c program to convert Celsius to Fahrenheit
- c program to convert Fahrenheit to Celsius
- c program to convert time in Hours:Minutes:Seconds to seconds
- c program to find the area of triangle given three sides
- c program to calculate Simple Interest
- c program to print multiplication table from 1 to n
- c program to check whether the given year is leap year or not
- c program to find the size of basic data types
- c program to find the area and circumference of a circle
- c program to find the roots of a quadratic equation
- c program to add two numbers
- c program to swap two numbers without using temporary variable
- c program to check whether the given number N is divisible by M
- c program to add two fractions
- c program to add two numbers without third variable
- c program to convert improper fraction to mixed fraction
- c program to convert centimeters to inches and feet
- c program to convert feet to inches
- c program to convert days into months and days
- c program to convert kilometers to miles, meters and feet
- c program to convert kilometer per hour to meter per second
- c program to convert kilograms to pounds and grams
- c program to convert minutes into hours
- c program to convert time in seconds to hours, minutes and seconds
- c program to convert hours, minutes to seconds
- c program to convert meter to feet and centimeters
- c program to convert plain to cipher text and cipher to plain text
- c program to convert pounds to kilograms
- c program to convert radians to degrees
- c program to convert year to roman equivalent
- c program to calculate compound interest
- c program to calculate power of a number
- c program to calculate factorial
- c program to find the area & perimeter of a circle, triangle, square and rectangle
- c program to calculate age in years, months and days
- c program to find absolute value of a given number
- c program to calculate age from date of birth
- c program to calculate body mass index
- c program to calculate distance between two points
- c program to calculate Greatest Common Divisor(GCD)
- c program to calculate Highest Common Factor(HCF)
- c program to calculate Least Common Multiple(LCM)
- c program to simplify the given fraction using GCF
- c program to calculate Gross and net salary of an employee
- c program to calculate Grades
- c program to calculate GPA
- c program to calculate income tax
- c program to calculate modulus
- c program to implement digital clock
- c program to calculate profit and loss
- c program to calculate percentage
- c program to calculate PI, resistance and power
- c program to calculate square root and square of a number
- c program to calculate volume of a cylinder and sphere
- c program to calculate weighted arithmetic mean
- c program to calculate x power y
- c program to check whether a date is valid or not
- c program to convert days into years, months and weeks
- c program to print date and sleep for a given time
- c program to clear screen and authenticate user
- c program to calculate generic root of any number
- c program to find largest digit of a number
- c program to generate multiplication table for the given number
- c program to convert decimal to binary and count the number of ones and zeros
- c program to round a number
- c program to guess a random number
- c program to implement ceaser cipher
- c program to swap nibble in a byte
- c program to find slope
- c program to calculate nCr and nPr
- c program to print even, odd and prime factors of a given number
- c program to print the four digit numbers whose sum of squares of first half and second half of the number equals the same number
- c program to check whether a given number is magic number or not
- c program to calculate correlation coefficient