Escolar Documentos
Profissional Documentos
Cultura Documentos
1 :
Program:
/* Program to accept a set of bivariate data , calculate the simple correlation coefficient ,
display the two regression equations and find regressed values of y given x and vice-versa */
#include<stdio.h>
#include<math.h>
void main()
{
int i,n,X_n,Y_n;
float x,y,sum1_x,sum2_x,sum1_y,sum2_y,prod_xy,mean_x,mean_y,var_x,var_y,
var_xy,r,b_yx,b_xy,reg_x[30],reg_y[30],X[30],Y[30]
char c,flag;
clrscr();
do
{
printf("Enter the number of paired observations:");
scanf("%d",&n);
}
while( n < 1);
sum1_x=sum2_x=sum1_y=sum2_y=prod_xy=0;
mean_x = sum1_x/n;
mean_y = sum1_y/n;
var_x = sum2_x/n - pow(mean_x,2);
var_y = sum2_y/n - pow(mean_y,2);
var_xy = prod_xy/n -mean_x * mean_y;
r = var_xy/sqrt(var_x * var_y);
b_yx = var_xy/var_x;
b_xy = var_xy/var_y;
}
else if( c == 'x')
{
do
{
printf("\nEnter the number of y values for which you want
to predict x:");
scanf("%d",&X_n);
if(X_n < 1 || X_n > 30)
{
printf("INVALID ENTRY");
}
}
while( X_n < 1 || X_n > 30);
for( i = 0; i < X_n; i++)
{
printf("\nEnter the value of y :");
scanf("%f",®_y[i]);
X[i] = mean_x + b_xy*(reg_y[i] - mean_y);
}
printf("\nPredictor value(y) Predicted value(X)");
for( i = 0; i < X_n; i++)
{
printf("\n%f %f",reg_y[i],X[i]);
}
printf("\nDo you want to calculate predicted values for a new data
set? Enter y if yes otherwise n :");
scanf("%s",&flag);
}
else
{
printf("\nINVALID ENTRY");
}
}
while( flag == 'y');
}
getch();