Correlation Method in C

Correlation is a statistical measure that indicates the extent to which two or more variables fluctuate together. A positive correlation indicates the extent to which those variables increase or decrease in parallel; a negative correlation indicates the extent to which one variable increases as the other decreases.

Program of Correlation in C


  #include<stdio.h>
  #include<conio.h>
  #include<math.h>
  void main()
  {
    float x[50],sum=0,y[50],flag1=0,flag2=0,meanx,meany;
    float s1=0,s2=0,s3=0,r=0,cov;
    int i,n;
    clrscr();
    printf("Enter the values of n :- ");
    scanf("%d",&n);
    printf("Enter the values of x :- \n");
    for(i=1;i<=n;i++)
    {
        scanf("%f",&x[i]);
        flag1=flag1+x[i];
    }
    printf("Enter the values of y :- \n");
    for(i=1; i<=n; i++)
    {
        scanf("%f",&y[i]);
        flag2=flag2+y[i];
    }
    meanx=flag1/n;
    meany=flag2/n;
    for(i=1; i<=n; i++)
    {
        sum=sum+(x[i]-meanx)*(y[i]-meany);
    }
    cov=sum/n;
    printf("covariance= %f",cov);
    for(i=1; i<=n; i++)
    {
        s1=s1+pow((x[i]-meanx),2);
        s2=s2+pow((y[i]-meany),2);
    }
    s3=sqrt(s1*s2);
    r=sum/s3;
    printf("\nCoeff. Of Correlation = %f",r);
    getch();
  }
  

Output

correlation method

Post Your Comment