Newer
Older
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#ifdef __CYGWIN__
#undef __STRICT_ANSI__
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
struct my_record {
char *time;
int session;
int speed;
int gid;
};
struct road_network {
int gid;
int type;
int speed;
};
struct aggregates {
int gid;
double speed [100];
int num;
int sess [100];
};
float harmonicMean(double arr[], int n) {
// Declare sum variables and initialize
// with zero.
double sum = 0;
for (int i = 0; i < n; i++)
sum = sum + (double) 1 / arr[i];
return (double) n / sum;
}
int unique_elements(int arr[], int len) {
int counted[len], j, n, count, flag;
counted[0] = arr[0];
count = 1; /*one element is counted*/
for (j = 0; j <= len - 1; ++j) {
flag = 1;
;
/*the counted array will always have 'count' elements*/
for (n = 0; n < count; ++n) {
if (arr[j] == counted[n]) {
flag = 0;
}
}
if (flag == 1) {
++count;
counted[count - 1] = arr[j];
}
}
return count;
}
double r2()
{
return (double)rand() / (double)RAND_MAX ;
}
double r3()
{
return ((double)rand() / (double)RAND_MAX )*90;
}
int main(int argc, char *argv[]) {
char mystring [100];
char mystring1 [100];
char *str;
char *str1;
printf("%s\n",argv[1]);
printf("%s\n",argv[2]);
FILE* my_file = fopen(argv[1], "r");
if (my_file == NULL) {
perror(argv[1]);
exit(1);
}
struct my_record records[10000];
size_t count = 0;
while (fgets(mystring, 100, my_file)!= NULL) {
str = strtok(mystring, ";");
records[count].time = str;
str = strtok(NULL, ";");
records[count].gid = atoi(str);
str = strtok(NULL, ";");
records[count].session = atoi(str);
str = strtok(NULL, ";");
records[count].speed = atoi(str);
str = strtok(NULL, ";");
++count;
}
fclose(my_file);
FILE* my_file1 = fopen(argv[2], "r");
if (my_file == NULL) {
perror(argv[1]);
exit(1);
}
struct road_network Trnava[30];
size_t count1 = 0;
while (fgets(mystring1, 100, my_file1)!= NULL) {
str1 = strtok(mystring1, ";");;
Trnava[count1].gid = atoi(str1);
str1 = strtok(NULL, ";");
Trnava[count1].type = atoi(str1);
str1 = strtok(NULL, ";");
Trnava[count1].speed = atoi(str1);
str1 = strtok(NULL, ";");
++count1;
}
fclose(my_file1);
struct aggregates Agg [30];
for (int i = 0; i<count1; i++) {
int temp = 0;
Agg[i].gid = Trnava[i].gid;
for (int j = 0; j<count; j++) {
if (records[j].gid == Trnava[i].gid) {
Agg[i].speed[temp] = records[j].speed;
Agg[i].sess[temp] = records[j].session;
}
temp++;
}
Agg[i].num = temp;
}
double means [count1];
int rel [count1];
for (int i = 0; i<count1; i++) {
if (Agg[i].speed[0] != 0) {
means[i] = harmonicMean(Agg[i].speed, Agg[i].num);
int tt = unique_elements(Agg[i].sess, Agg[i].num);
rel[i] = tt / ((1 / Trnava[i].type + 1)*100);
} else {
means[i] = -1;
rel[i] = -1;
}
}
FILE* my_file3 = fopen(argv[3], "w");
if (my_file3 == NULL) {
exit(1);
}
fprintf(my_file3, "%s;", records[0].time);
fprintf(my_file3, "%f", rel[0]);
for (int i = 1; i < count1; i++) {
fprintf(my_file3, ";%f", rel[i]);
}
fprintf(my_file3, "\n");
fclose(my_file3);
FILE* my_file4 = fopen(argv[4], "w");
if (my_file4 == NULL) {
exit(201);
}
fprintf(my_file4,"%s;", records[0].time);
fprintf(my_file4,"%f", means[0]);
for (int i = 0; i<count1; i++) {
fprintf(my_file4,";%f", means[i]);
}
fprintf(my_file4,"\n");
fclose(my_file4);
FILE* my_file5 = fopen(argv[5], "w");
if (my_file5 == NULL) {
exit(201);
}
fprintf(my_file5,"%d ", Trnava[0].gid);
for (int i = 1; i<count1; i++) {
fprintf(my_file5,";%d", Trnava[i].gid);
}
fprintf(my_file5,"\n");
for (int j=0;j<2016;j++){
fprintf(my_file5,"%f", r2);
for (int i = 1; i<count1;i++) {
fprintf(my_file5,";%f", r2);
}
fprintf(my_file5,"\n");
}
fclose(my_file5);
FILE* my_file6 = fopen(argv[6], "w");
if (my_file6== NULL) {
exit(201);
}
fprintf(my_file6,"%d", Trnava[0].gid);
for (int i = 1; i<count1; i++) {
fprintf(my_file6,"%d;", Trnava[i].gid);
}
fprintf(my_file6,"\n");
for (int j=0;j<2016;j++){
fprintf(my_file6,"%f;", r3);
for (int i = 0; i<count1;i++) {
fprintf(my_file6,";%f", r3);
}
fprintf(my_file6,"\n");
}
fclose(my_file6);
}