Logo 
Search:

C Programming Articles

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

Program to find sum, average and deviation of input numbers using parallel processing

Posted By: Verena Fischer     Category: C Programming     Views: 5663

Write a Program to find sum, average and deviation of input numbers using parallel processing.

Code for Program to find sum, average and deviation of input numbers using parallel processing in C Programming

#include "headfork.h"// included in attachment
#include "headsem.h"//#include "headbar.h"
#include "headshr.h"
#include<stdio.h>
#include<math.h>

main()
{
        float a[10];
        int *lock,*bar,shmid1,shmid2,shmid3;
        float *avg,*dev,sum;
        int id,n,np,i;

    avg=(float *)shared(sizeof(float)*2,&shmid1);
        dev=(float *)shared(sizeof(float)*2,&shmid2);
        lock=shared(sizeof(int)*2,&shmid3);

        printf("Enter the limit of array :" );
        scanf("%d",&n);

        printf("Enter the elemts of array:\n");
        for(i=0;i<n;i++)
                scanf("%f",&a[i]);

    printf("Enter the no.of proc: ");
    scanf("%d",&np);
    
        *avg=0.0;
        *dev=0.0;

//    bar=bar_init(np);
        
    id=p_fork(np);
        sum=0.0;

        for(i=id;i<n;i+=np)
                sum+=a[i];
        
    locksem(lock);
        *avg=*avg + sum/(float)n;
        unlock(lock);

//    barrier(bar);
        sum=0.0;

        for(i=id;i<n;i+=np)
                sum=sum + abs(a[i] - *avg);

        locksem(lock);
        *dev=*dev + sum/(float)n;
    unlock(lock);
    
    p_join(np,id);

        printf("Deviation : %f",*dev);
}
  
Share: 



Verena Fischer
Verena Fischer author of Program to find sum, average and deviation of input numbers using parallel processing 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!