Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- int compare (const void * a, const void * b)
- {
- if (*(double *)a > *(double *)b) return 1;
- if (*(double *)a < *(double *)b ) return -1;
- return 0;
- }
- int main()
- {
- double X[] = { 62.0,53.0,51.0,25.0,79.2,23.7,47.9,24.1 };
- double Y[] = { 52,64.5,39.9,34,21.5,37,39,65.8 };
- const int LENGTH=8;
- // copy arrays
- double origX[LENGTH];
- double origY[LENGTH];
- for (int i=0; i<LENGTH; i++)
- {
- origX[i]=X[i];
- origY[i]=Y[i];
- }
- qsort(X, LENGTH, sizeof(double), compare);
- qsort(Y, LENGTH, sizeof(double), compare);
- printf("Sorted data\n");
- for (int i=0; i<LENGTH; i++)
- printf("%d %f %f\n",i, X[i], Y[i]);
- // replace values by ranks
- for (int i=0; i<LENGTH; i++)
- {
- double val = origX[i];
- for (int j=0; j<LENGTH; j++)
- {
- if (X[j]==val)
- {
- origX[i]=j;
- break;
- }
- }
- val = origY[i];
- for (int j=0; j<LENGTH; j++)
- {
- if (Y[j]==val)
- {
- origY[i]=j;
- break;
- }
- }
- }
- printf("Ranks\n");
- for (int i=0; i<LENGTH; i++)
- printf("%d %f %f \n",i, origX[i], origY[i]);
- // form sum of difference in rank, squared
- int sumD2=0;
- for (int i=0; i<LENGTH; i++)
- {
- int d = (int)(origX[i]-origY[i]);
- sumD2+=d*d;
- }
- // r = 1 - 6 sunD2 / (n^3-n)
- double r = 1 - 6.0*sumD2/ (LENGTH*(LENGTH*LENGTH-LENGTH));
- printf("Pearson's rank = %f\n", r);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment