Logo 
Search:

C Programming Articles

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

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

Posted By: Easy Tutor     Category: C Programming     Views: 3155

Write a program to calculate average of the elements of an array and then the average deviation using Race condition).

Code for Program to calculate average of the elements of an array and then the average deviation using Race condition) in C Programming

# include <stdio.h>
# include <math.h>
# include "/usr/include/sys/types.h"
# include "/usr/include/sys/shm.h"
# include "/usr/include/sys/ipc.h"
# include "/usr/include/sys/sem.h"
# include "forkjoin.h"
# include "sharedlib.h"
# include "spinlock.h"int main()
{
    int arr[100];
    int arrSize;        //Size of arrayint iCount;        //Counter Variableint id;            //Process IDint nProc=3;        //number of Processesfloat sum=0;
    
    float *deviation,*avg;  // Shared Variablesint *lock;        // Shared Variable for Spinlockint shmidavg,shmidlock,shmiddeviation;    // Shmid for Shared Variables
    
    printf("Enter the Size of an Array :");
    scanf("%d",&arrSize);

    for(iCount=0;iCount<arrSize;iCount++)
    {
        printf("Enter arr[%d] :",iCount);
        scanf("%d",&arr[iCount]);
    }

    /* Allocate Shared memory */
    avg=(float*)sshared(sizeof(float),&shmidavg);
    deviation=(float*)sshared(sizeof(float),&shmiddeviation);
    lock=(int*)sshared(sizeof(int),&shmidlock);

    spin_lock_init(lock);        // Spin Lock Initialization
    *avg=0;

    id=process_fork(nProc);        // Forking Processes/* Partial Sum using Loop Spliting */for(iCount=id;iCount<arrSize;iCount=iCount+nProc)
    {
        sum = sum + arr[iCount];
    }

    spin_lock(lock);
        /* Critical Region */
        *avg = *avg + ( sum /arrSize);    // Calculate Average from Partial Sums
    spin_unlock(lock);

    /*    ---------------------------------------------------------------------------    Barrier Should be here, So that all Processes have to wait until Final    Average is Calculated.    ---------------------------------------------------------------------------    */
    
    sum=0;
    /* Calculate Sum(Xi - Mean) Using Loop Spliting */for(iCount=id;iCount<arrSize;iCount=iCount+nProc)
    {        
        sum = sum + pow(((float)arr[iCount] - *avg),2);
    }
    
    spin_lock(lock);
        /* Critical Region */
        *deviation = *deviation + (sum / arrSize-1); // Calculate Final Deviation
    spin_unlock(lock);

    
    process_join(nProc,id);        // Joining the Process

    *deviation=sqrt(*deviation);
    printf("Deviation : %f\n",*deviation);

    /* Cleaning the Shared Region */
    cleanup_memory(&shmidavg);
    cleanup_memory(&shmidlock);
    cleanup_memory(&shmiddeviation);    
    return 0;
}
  
Share: 



Easy Tutor
Easy Tutor author of Program to calculate average of the elements of an array and then the average deviation using Race condition) is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
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!