Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Parallel Processing ProgramsRSS Feeds

PROGRAM TO FIND CORRELATION BETWEEN TWO ARRAYS USING BARRIER AND LOOP-SPLITTING

Posted By: Malfreda Schmidt     Category: C Programming     Views: 2431

PROGRAM TO FIND CORRELATION BETWEEN TWO ARRAYS USING BARRIER AND LOOP-SPLITTING.

Code for PROGRAM TO FIND CORRELATION BETWEEN TWO ARRAYS USING BARRIER AND LOOP-SPLITTING in C Programming

#include <stdio.h>
#include <math.h>
#include "forkjoin.h"
#include "shared.h"
#include "barrier.h"int main()
{
        int *x,*y,*bar1,shmid1,shmid2,shmid3;
        int shmid4,shmid5,shmid6,size,sum,sum1;
        int shmid7,shmid8,nproc,k,pid,i,l;
        float *mean1,*mean2,*xy,*x1,*y1, total;
        size=5,nproc=3,total=0;
        x=(int*)create_memory(2*size,&shmid1);
        y=(int*)create_memory(2*size,&shmid2);
        x1=(float*)create_memory(4*size,&shmid3);
        y1=(float*)create_memory(4*size,&shmid4);
        xy=(float*)create_memory(4*size,&shmid5);
        mean1=(float*)create_memory(4,&shmid6);
        mean2=(float*)create_memory(4,&shmid7);
         bar1=(int*)create_memory(2*4,&shmid8);
        for(i=0;i<size;i++)
        {
                printf("Enter the value of x of %d:: ",i+1);
                scanf("%d",&x[i]);
                printf("Enter the value of y of %d:: ",i+1);
                scanf("%d",&y[i]);
        }
        barrier_init(bar1,nproc);
        (*mean1)=(*mean2)=0;
        pid=create_process(&nproc);
        sum=sum1=0;
        for(k=pid;k<size;k+=nproc)
        {
                sum1 = sum1 + y[k];
                sum = sum + x[k];
        }
        (*mean1)+=(sum/(float)size);
        (*mean2)+=(sum1/(float)size);
        barrier(bar1);
        for(k=pid;k<size;k+=nproc)
        {
                x1[k]=(x[k])-(*mean1);
       y1[k]=(y[k])-(*mean2);
        }
        barrier(bar1);
        for(k=pid;k<size;k+=nproc)
        xy[k]=(x1[k])*(y1[k]);
        join_process(&nproc,&pid);
        fflush(stdout);
        for(l=0;l<size;l++)
        {
                total+=(xy[l]);
        }
        printf("\nTotal is %f\n",total);
        if(total<0)
                printf("Correlation is negative\n");
        elseif(total>0)
                printf("Correlation is positive\n");
        else
                printf("Correlation cannot be defined\n");
        cleanup_memory(&shmid1);
        cleanup_memory(&shmid2);
        cleanup_memory(&shmid3);
        cleanup_memory(&shmid4);
        cleanup_memory(&shmid5);
         cleanup_memory(&shmid6);
        cleanup_memory(&shmid7);
        cleanup_semaphore(&bar1[3]);
        cleanup_memory(&shmid8);
        return 0;
}

OUTPUT:-

Enter the value of x of 1:: 1
Enter the value of y of 1:: 10
Enter the value of x of 2:: 2
Enter the value of y of 2:: 20
Enter the value of x of 3:: 3
Enter the value of y of 3:: 30
Enter the value of x of 4:: 4
Enter the value of y of 4:: 40
Enter the value of x of 5:: 5
Enter the value of y of 5:: 50

Total is 100.000000
Correlation is positive
  
Share: 



Malfreda Schmidt
Malfreda Schmidt author of PROGRAM TO FIND CORRELATION BETWEEN TWO ARRAYS USING BARRIER AND LOOP-SPLITTING is from Frankfurt, Germany.
 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!