C Programming Tutorial

 
 
 
 
#include <stdio.h>
#include \"forkjoin.h\"
#include \"shared.h\"
#include \"barrier.h\"

int main()
{
       int *a,i,shmid,size,nproc,pid,sum,shmid3,shmid1,condition;
       int *lock,shmid4;

       float *mean,*dev;

       printf(\"Enter the size of an array : \");
       scanf(\"%d\",&size);
       printf(\"Enter the no of process  : \");
       scanf(\"%d\",&nproc);
       a = (int *) create_memory(2 *size,&shmid);
       mean = (float *) create_memory(sizeof(float),&shmid3);
       lock = (int *) create_memory(2,&shmid4);
       condition = 0;
       spin_lock_init(lock,&condition);
       printf(\"Enter the elements of an array :\\n \");
       for(i=0;i<size;i++)
       {
              printf(\"\\nEnter the %d Element : \",i+1);
              scanf(\"%d\",&a[i]);
       }
       *mean =0;
       pid = create_process(&nproc);
       sum =0;
       for(i=pid;i<size;i+=nproc)
          sum = sum + a[i];
       spin_lock(lock);
       (*mean)+=(sum/(float)size);
       spin_unlock(lock); 
       join_process(&nproc,&pid); 
       printf(\"The Mean of an array is %f \\n\" ,*mean);
       cleanup_memory(&shmid);
       cleanup_memory(&shmid3); 
       cleanup_memory(&shmid1);
       cleanup_semaphore(lock);
    return 0; 

 } 

/*****************************************************************

               :: OUTPUT ::

    Enter the size of an array : 5
    Enter the no of process  : 3

    Enter the elements of an array :

    Enter the 1 Element : 11
    Enter the 2 Element : 22
    Enter the 3 Element : 33
    Enter the 4 Element : 44
    Enter the 5 Element : 55

    The Mean of an array is 33.000000 

****************************************************************/ 

 
 
Didn't find what you were looking for? Find more on PROGRAM TO FIND THE MEAN OF USER DEFINED NUMBER OF ARRAY ELEMENTS