Logo 
Search:

C Programming Articles

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

PROGRAM TO FIND THE MEAN OF USER DEFINED NUMBER OF ARRAY ELEMENTS

Posted By: Evelyn Hughes     Category: C Programming     Views: 3676

PROGRAM TO FIND THE MEAN OF USER DEFINED NUMBER OF ARRAY ELEMENTS.

Code for PROGRAM TO FIND THE MEAN OF USER DEFINED NUMBER OF ARRAY ELEMENTS in C Programming

#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

****************************************************************/
  
Share: 



Evelyn Hughes
Evelyn Hughes author of PROGRAM TO FIND THE MEAN OF USER DEFINED NUMBER OF ARRAY ELEMENTS is from London, United Kingdom.
 
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!