-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab6_1.c
125 lines (119 loc) · 2.01 KB
/
lab6_1.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <pthread.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define Num_Threads 5
int min,max,med,argno;
float avg,sd;
void *maximum(void *a)
{
int i,*ar=(int *)a;
max=ar[0];
for(i=0;i<argno-1;i++)
{
if(ar[i]>max)
{
max=ar[i];
}
else
{
continue;
}
}
pthread_exit(0);
}
void *minimum(void *a)
{
int i,*ar=(int *)a;
min=ar[0];
for(i=0;i<argno-1;i++)
{
if(ar[i]<min)
{
min=ar[i];
}
else
{
continue;
}
}
pthread_exit(0);
}
void *median(void *a)
{
int i,j,*ar=(int *)a;
for(i=0;i<argno-1;i++)
{
for(j=0;j<i;j++)
{
if(ar[j]>ar[i])
{
int temp=ar[i];
ar[i]=ar[j];
ar[j]=temp;
}
}
}
med=ar[(argno-1)/2];
pthread_exit(0);
}
void *standard_deviation(void *a)
{
int i,*ar=(int *)a,sed=0,mean=0,sum=0;
for(i=0;i<argno-1;i++)
{
sum+=ar[i];
}
mean=sum/(argno-1);
for(i=0;i<argno-1;i++)
{
sed+=(ar[i]-mean)*(ar[i]-mean);
}
sd=sqrt(sed/(argno-2));
pthread_exit(0);
}
void *average(void *a)
{ int i,*ar=(int *)a,sum=0;
for(i=0;i<argno-1;i++)
{
sum+=ar[i];
}
avg=sum/(argno-1);
pthread_exit(0);
}
int main(int argc, char const *argv[])
{
argno=argc;
int a[1000];
pthread_t workers[Num_Threads];
pthread_attr_t attr;
if(argc==1)
{
fprintf(stderr,"usage: a.out <integer value>\n");
return -1;
}
if(atoi(argv[1])<0)
{
fprintf(stderr,"%d must be >=0\n",atoi(argv[1]));
return -1;
}
for(int i=1;i<argc;i++)
{
a[i-1]=atoi(argv[i]);
}
pthread_attr_init(&attr);
pthread_create(&workers[0],&attr,average,(void *)a);
pthread_create(&workers[1],&attr,maximum,(void *)a);
pthread_create(&workers[2],&attr,minimum,(void *)a);
pthread_create(&workers[3],&attr,median,(void *)a);
pthread_create(&workers[4],&attr,standard_deviation,(void *)a);
for(int o=0;o<Num_Threads;o++)
{
pthread_join(workers[o],NULL);
}
printf("The Average value is:%f\n",avg);
printf("The minimum value is:%d\n",min);
printf("The maximum value is:%d\n",max);
printf("The standard deviation value is:%f\n",sd);
printf("The median value is:%d\n",med);
}