-
Notifications
You must be signed in to change notification settings - Fork 1
/
ue01.c
265 lines (206 loc) · 5.97 KB
/
ue01.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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
* File: uebung01_bsp03.c
* Author: nina
*
* Created on 5. März 2014, 16:49
*/
#include <stdlib.h> // error handling
#include <stdio.h> // file io
#include <math.h> // sqrt()
#include <string.h>
#define NQUESTIONS 20 ///< Number of questions
unsigned int myrand()
{
static unsigned int randomNumber=2;
randomNumber = ((7*randomNumber+4)%17);
return randomNumber;
}
int numberRightAnswersPerson(const char *filename, int *answers)
{
FILE* fCurrent; // File Handle
int cq=0; // Buffers for file parsing
int cansw=0; // storage answer
int rightAnswersPerson=0;
fCurrent = fopen(filename, "r");
if( fCurrent == NULL )
{
return -1;
}
// READ FILE
do
{
cq = fgetc(fCurrent); //stores question
if(cq>='A' && cq<='T')
{
cansw = fgetc(fCurrent)-'0'; //stores answer
if(answers[cq-'A']==cansw)
{
++rightAnswersPerson;
}
}
} while (cq!=EOF);
// CLOSE FILE
fclose(fCurrent);
return rightAnswersPerson;
}
int numberRightAnswersAllPersons(const char *filename, int *sumRightAnswers, int *answers)
{
FILE* fCurrent; // File Handle
int cq=0; // Buffers for file parsing
int cansw=0; // storage answer
fCurrent = fopen(filename, "r");
if( fCurrent == NULL )
{
return -1;
}
// READ FILE
do
{
cq = fgetc(fCurrent); //stores question
if(cq>='A' && cq<='T')
{
cansw = fgetc(fCurrent)-'0'; //stores answer
if(answers[cq-'A']==cansw)
{
sumRightAnswers[cq-'A']+=1;
}
}
} while (cq!=EOF);
// CLOSE FILE
fclose(fCurrent);
return 0;
}
double calcAvg(int *r, int n)
{
int s=0;
for(int i=0;i<n;++i)
{
s+=r[i];
}
return (double)s/n;
}
double calcStdDev(double avg, int *r, int n)
{
double s=0;
for(int i=0;i<n;++i)
{
s+=(r[i]-avg)*(r[i]-avg);
}
return sqrt(s/(n-1));
}
int cmpIntAsc(const void * a, const void * b)
{
int x = *(const int *)a;
int y = *(const int *)b;
if (x < y) return -1;
else if (x > y) return 1;
else return 0;
}
double calcMedian(int *r, int n)
{
int *a = (int *)malloc(n * sizeof(int));
double m;
memcpy(a,r,n*sizeof(int));
qsort(a,n,sizeof(int),cmpIntAsc); //sorts array, zB a[0],a[1] -> a und b in cmp-funktion
if((n%2)!=0)
{
m=(double)a[n/2];
}
else
{
m=(double)((a[n/2]+a[n/2-1])/2.0);
}
if(a!=0)
{
free(a);
a=0;
}
return m;
}
int main(int argc, char** argv)
{
// DECLARATIONS
int numParticipants=argc-1;
int *answers=(int *)malloc(NQUESTIONS*sizeof(int));
if(answers==0)
{
fputs("Malloc (answers) returned null!\n",stderr);
return -1;
}
// CRUDE ARGUMENT CHECKING
if(argc<2) fputs("Error: No argument supplied!\n", stderr);
for(int i=0;i<NQUESTIONS;++i)
{
answers[i]=(myrand()%4+1);
}
///////////////////////////////////////////////////////////////////
// 3a) how many right answered question a single participant have?
///////////////////////////////////////////////////////////////////
FILE *f;
int *r=(int *)malloc(numParticipants*sizeof(int));
if(r==0)
{
fputs("Malloc (r) returned null!\n",stderr);
return -1;
}
f=fopen("rightanswers_pp.txt","w");
fprintf(f,"number of right answers per participant:\n");
for(int i=0;i<numParticipants;++i)
{
r[i]=numberRightAnswersPerson(argv[i+1], answers);
fprintf(f,"participant %d : %d \n", i+1, r[i]); //write right answer per participant in file
}
fclose(f);
///////////////////////////////////////////////////////////////////
// 3c)1) average value, deviation value, median
///////////////////////////////////////////////////////////////////
//average
double rAvg=calcAvg(r,numParticipants);
printf("average how many right answered question per person %lf\n",rAvg);
//standard deviation
printf("standard deviation: %lf\n",calcStdDev(rAvg,r,numParticipants));
//median
printf("median: %lf\n",calcMedian(r,numParticipants));
printf("\n");
///////////////////////////////////////////////////
// 3b) calculate right answers of all per question
///////////////////////////////////////////////////
FILE *f2;
int sumRightAnswers[NQUESTIONS];
for(int i=0;i<NQUESTIONS;++i)
{
sumRightAnswers[i]=0;
}
f2=fopen("rightanswers_all.txt","w");
fprintf(f2,"number of all right answers per question:\n");
for(int i=0;i<numParticipants; ++i)
{
if (numberRightAnswersAllPersons(argv[i+1],sumRightAnswers, answers)!=0)
{
perror("Error reading file");
}
}
for(int i=0;i<NQUESTIONS;++i)
{
fprintf(f2,"%c : %d\n",'A'+i,sumRightAnswers[i]);
}
fclose(f2);
//average how many persons answered a single question right
double av_all=calcAvg(sumRightAnswers,NQUESTIONS);
printf("average how many persons answered a single question right: %lf\n",av_all);
//standard deviation
printf("standard deviation: %lf\n",calcStdDev(av_all,sumRightAnswers,NQUESTIONS));
//median
printf("median: %lf\n",calcMedian(sumRightAnswers,NQUESTIONS));
if(answers!=0)
{
free(answers);
answers=0;
}
if(r!=0)
{
free(r);
r=0;
}
return EXIT_SUCCESS;
}