#include<stdio.h>
#include "shared.h"
#include "barrier.h"
#include "forkjoin.h"int main()
{
int size, i, nproc, pid, *bar, *x, *y, sx=0, sy=0;
int sh1, sh2, sh3, sh4, sh5, sh6, sh7, sh8;
float *meanx, *meany, *X, *Y, *XY;
printf("Enter the total number of processes :: ");
scanf("%d",&nproc);
printf("Enter the size of the array :: ");
scanf("%d",&size);
x=(int *)create_memory(size*2,&sh1);
y=(int *)create_memory(size*2,&sh2);
meanx=(float *)create_memory(4,&sh3);
meany=(float *)create_memory(4,&sh4);
*meanx = *meany = 0;
bar=(int *)create_memory(4*2,&sh5);
barrier_init(bar,nproc);
XY=(float *)create_memory(4,&sh6);
*XY=0;
X=(float *)create_memory(4,&sh7);
Y=(float *)create_memory(4,&sh8);
printf("Enter the elements for first array\n",i+1);
for(i=0;i<size;i++)
scanf("%d",&x[i]);
printf("Enter the elements for second array\n");
for(i=0;i<size;i++)
scanf("%d",&y[i]);
pid=create_process(&nproc);
for(i=pid;i<size;i+=nproc)
{
sx += x[i];
sy += y[i];
}
*meanx += (float)sx/size;
*meany += (float)sy/size;
barrier(bar);
for(i=pid;i<size;i+=nproc)
{
X[i] = (float)(x[i] - *meanx);
Y[i] = (float)(y[i] - *meany);
}
barrier(bar);
for(i=pid;i<size;i+=nproc)
*XY += X[i]*Y[i];
join_process(&nproc,&pid);
if(*XY > 0)
printf("The corelation between two data sets is positive.");
elseif(*XY < 0)
printf("The corelation between two data sets is negative.");
cleanup_memory(&sh1);
cleanup_memory(&sh2);
cleanup_memory(&sh3);
cleanup_memory(&sh4);
cleanup_memory(&sh5);
cleanup_memory(&sh6);
cleanup_memory(&sh7);
cleanup_memory(&sh8);
return 0;
}
/* OUTPUT */
Enter the total number of processes :: 3
Enter the size of the array :: 5
Enter the elements for first array
12
14
2
1
3
Enter the elements for second array
1
2
3
4
5
The corelation between two data sets is negative.