Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <pthread.h>
- #include <stdlib.h>
- int rows;
- void *routine(void * num);
- int main(int argc,char *argv[]){
- int lines =atoi(argv[1]);
- rows =atoi(argv[2]);
- int **A;
- int i,j,k,mo;
- pthread_t thread;
- mo=0;
- A=(int **)calloc(lines,sizeof(int*));
- for(i=0; i<lines;i++){
- A[i]=(int *)calloc(rows,sizeof(int));
- }
- for(i=0; i<lines;i++){
- for(j=0; j<rows;j++){
- A[i][j]=rand() % 10;
- //A[i][j]=2;
- }
- }
- for(j=0; j<lines; j++){
- pthread_create(&thread,NULL,routine,(void *)*(A+j));
- }
- for(k=0; k<lines;k++){
- pthread_join(thread,NULL);
- }
- for(j=0; j<lines; j++){
- mo += *(*(A+j));
- }mo=mo/lines;
- printf("%d\n",mo);
- for(i=0; i<lines;i++){
- free(A[i]);
- }free(A);
- }
- void *routine(void * num){
- int i,sum,average;
- sum=0;
- for (i=0; i<rows;i++){
- sum += *(int *)num ;
- }
- average= sum/rows;
- *(int*)num=average;
- printf("%d\n",average);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement