SHARE
TWEET

Untitled

a guest Apr 18th, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <pthread.h>
  3. #include <stdlib.h>
  4. int rows;
  5. void *routine(void * num);
  6. int main(int argc,char *argv[]){
  7.    
  8.     int lines =atoi(argv[1]);
  9.     rows =atoi(argv[2]);
  10.     int **A;
  11.     int i,j,k,mo;
  12.      pthread_t thread;
  13.     mo=0;
  14.  
  15.     A=(int **)calloc(lines,sizeof(int*));
  16.         for(i=0; i<lines;i++){
  17.             A[i]=(int *)calloc(rows,sizeof(int));
  18.         }
  19.     for(i=0; i<lines;i++){
  20.         for(j=0; j<rows;j++){
  21.             A[i][j]=rand() % 10;
  22.             //A[i][j]=2;
  23.         }
  24.     }
  25.     for(j=0; j<lines; j++){
  26.         pthread_create(&thread,NULL,routine,(void *)*(A+j));
  27.  
  28.     }
  29.     for(k=0; k<lines;k++){
  30.         pthread_join(thread,NULL);
  31.     }
  32.     for(j=0; j<lines; j++){
  33.         mo += *(*(A+j));
  34.     }mo=mo/lines;
  35.     printf("%d\n",mo);
  36.    
  37.     for(i=0; i<lines;i++){
  38.         free(A[i]);
  39.     }free(A);
  40. }
  41. void *routine(void * num){
  42.     int i,sum,average;
  43.     sum=0;
  44.     for (i=0; i<rows;i++){
  45.           sum += *(int *)num ;
  46.     }
  47.     average= sum/rows;
  48.     *(int*)num=average;
  49.     printf("%d\n",average);
  50.  
  51. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top