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 

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

    Related Post:
  1. Program that restarts your computer

  2. Program to find co-relation between two data sets defined and initialise by the user

  3. Program to obtain roots of a quadratic equation

  4. Program to interchange the Small and Captial Letters on the screen after every 5th second and Unload the TSR on a Delete Key Press

  5. Program to take input of 5 candidates vote and display it using an array

  6. Program to harmonic series

  7. Program of hanoi tower

  8. Program to print values of 2 raise to power N, N and 2 raise to power -n

  9. Program of histogram by calculating the partial histograms and consolidating in the end to get the final histogram

  10. Program to do sum of elements of an array by Loop Splitting

  11. Function to find the binary equivalent of a given decimal integer and display it

  12. Program to calculate average of the elements of an array and then the average deviation using race condition

  13. Program to find matrix addition, subtraction, multiplication, transpose and symmetric operations

  14. PROGRAM TO FIND ANSWER OF THE FOLLOWING SERIES. INPUT STEPS FROM KEYBORD SUM =12-32+52-72+.....

  15. Program that illustrates the function of %[ ] specification

  16. Program to count the total no of characters in one or more character strings by any parallel technique

  17. String handling by pointers

  18. Singly Linked list with following operations INSERT AT STARTING, INSERT AT MIDDLE, INSERT AT END, DELETE FIRST NODE, DELETE LAST NODE, DELETE MIDDLE

  19. Program that performs add, edit, delete, display and search data from file

  20. Example of passing variable values to a function

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