C Programming Tutorial

 
 
 
 
#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\");
        else if(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

    Related Post:
  1. PROGRAM TO FIND ANSWER OF THE FOLLOWING SERIES. INPUT STEPS FROM KEYBOARD SUM =1+4-9+16-25+36-.....

  2. Program to read person details and display records whose weight is greater than 60 using structure

  3. PROGRAM TO FIND SUM OF ALL INTEGER WHICH IS &gt; 100 AND &lt; 200 AND WHICH IS DIVISIBLE BY 7

  4. PROGRAM TO READ NUMBERS, N1 AND N2 ARE INTERCHANGE WITHOUT USING EXTRA VARIABLES.N1=10, N2=20 THEN O/P IS N1=20, N2=10 USING MULTIPLICATION, DIVISION

  5. Program to show the implementation of Double Mouse Click

  6. Program that illustrates the use of malloc and calloc functions

  7. Program to print triangle inside square using * (stars)

  8. Selection sort

  9. Program to concatenate one linked list at end of another and than to erase all nodes present in the linked list

  10. Program to compare one string s2 to another string s1. Print the difference of first unmatched characters (Without Using strcmp function)

  11. Program to sum the digit of each digit

  12. Program to find the area of square

  13. Summation of an even numbers

  14. PROGRAM TO FIND ANSWER OF THE FOLLOWING SERIES. SUM=e1+e2+e3+...

  15. Program to calculate average of the elements of an array and then the average deviation using Race condition)

  16. Program to exchange numbers using pointers

  17. PROGRAM USNG WHILE LOOP FOR THE FIBONANSI SERIES IN TRIANGLE SERIES

  18. Program to get answer of inventor of c. Allow user to give answer in 3 attempts

  19. program that takes a number from user and calculates its logarithm value to the base 10 and e, exponentiation, sin value, cosine value and square root

  20. Program of string variable

 
 
Didn't find what you were looking for? Find more on PROGRAM TO FIND CORRELATION BETWEEN TWO ARRAYS USING BARRIER AND LOOP-SPLITTING