Logo 
Search:

C Programming Articles

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

PROGRAM TO FIND CORRELATION BETWEEN TWO DATA SETS DEFINED NUMBER OF PROCESSES

Posted By: Adelino Fischer     Category: C Programming     Views: 3838

PROGRAM TO FIND CORRELATION BETWEEN TWO DATA SETS DEFINED NUMBER OF PROCESSES.

Code for PROGRAM TO FIND CORRELATION BETWEEN TWO DATA SETS DEFINED NUMBER OF PROCESSES in C Programming

#include <stdio.h>
#include "forkjoin.h"
#include "shared.h"
#include "barrier.h"int main()
{
       int *a,*b,pid,nproc,i,*bar,sum1,sum2,size;
       int shmid1,shmid2,shmid3,shmid4,shmid5,shmid6,shmid7,shmid8;
       float *a1,*b1,*ab,*mean1,*mean2,total;

       printf("Enter the size of the array : ");
       scanf("%d",&size);
       a = (int *) create_memory(2*size,&shmid1);
       b = (int *) create_memory(2*size,&shmid2);
       a1 = (float *) create_memory(4*size,&shmid4);
       b1 = (float *) create_memory(4*size,&shmid5);
       ab = (float *) create_memory(4*size,&shmid6);
       mean1 = (float *) create_memory(4,&shmid7);
       mean2 = (float *) create_memory(4,&shmid8);
       bar = (int *) create_memory(2*4,&shmid3);
       
       for(i=0;i<size;i++)        
       {
             printf("Enter %d Element of x : ",i+1);
             scanf("%d",&a[i]);
             printf("Enter %d Element of y : ",i+1);
             scanf("%d",&b[i]);  
       }
       printf("Enter the no of process : ");
       scanf("%d",&nproc);
       barrier_init(bar,nproc);
       sum1=0;
       sum2=0;
       pid=create_process(&nproc);
       for(i=pid;i<size;i+=nproc)
       {
             sum1 = sum1 + a[i];
             sum2 = sum2 + b[i];
       } 
       barrier(bar);

       (*mean1) += (float)sum1/size;
       (*mean2) += (float)sum2/size;

       barrier(bar);
       for(i=pid;i<size;i+=nproc)
       {    
             a1[i] = (a[i])-(*mean1);
             b1[i] = (b[i])-(*mean2);
       }
       for(i=pid;i<size;i+=nproc)
       {
             ab[i] = a1[i]*b1[i];
       }
       join_process(&nproc,&pid);
       for(i=0;i<size;i++)
       {
             total = total+ab[i];
       } 
       printf("The Correlation is : %f \n",total);
       if(total >= 0)
             printf("The Correlation  is Positive\n"); 
       else  
             printf("The Correlation is Negative \n");
       cleanup_memory(&shmid1);
       cleanup_memory(&shmid2);
       cleanup_memory(&shmid3);
       cleanup_memory(&shmid4);
       cleanup_memory(&shmid5);
       cleanup_memory(&shmid6);
       cleanup_memory(&shmid7);
       cleanup_memory(&shmid8);

return 0;              
     
}  
       

          :: OUTPUT ::

    Enter the size of the array : 5

    Enter 1 Element of x : 11
    Enter 1 Element of y : 1
    Enter 2 Element of x : 12
    Enter 2 Element of y : 2
    Enter 3 Element of x : 13
    Enter 3 Element of y : 3
    Enter 4 Element of x : 14
    Enter 4 Element of y : 4
    Enter 5 Element of x : 15
    Enter 5 Element of y : 5

    Enter the no of process : 3

    The Correlation is : 8.000206 
    The Correlation  is Positive
  
Share: 



Adelino Fischer
Adelino Fischer author of PROGRAM TO FIND CORRELATION BETWEEN TWO DATA SETS DEFINED NUMBER OF PROCESSES is from Frankfurt, Germany.
 
View All Articles

 
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!