Universität Ulm,
Fakultät für Mathematik und Wirtschaftswissenschaften,
SAI
Lösung zu Blatt 2
der Systemnahe Software I (WS 97/98)
Hanutabildchen
#include<stdio.h>
#include<stdlib.h>
#include<time.h> /* Uhrzeit fuer srand */
#include<values.h> /* Fuer maxint */
int gug(int bild[]) { /* Teste ob alle Bilder gekauft */
int i, sum = 0;
for (i = 0; i <= 10; i++)
sum += bild[i];
return(sum);
}
int zufall() {
int i = 0, bild[11] = {0,0,0,0,0,0,0,0,0,0,0};
while (gug(bild) != 11) {
/* Fuer Aufgabe 3: */
/* bild[rand() % 11] = 1; */
/* Fuer Aufgabe 4: */
bild[(rand() % 21) % 11] = 1;
i++;
}
return(i);
}
void main() {
long start;
int next, anz, i, min = MAXINT, max = 0, avg = 0;
printf("Bitte Anzahl der Simulationen eingeben: ");
scanf("%d",&anz);
time(&start);
srand(start);
for (i = 0; i < anz; i++) {
next = zufall();
min = ((min < next)?min:next);
max = ((max > next)?max:next);
avg += next;
}
printf("Sie muesen min %d, avg %5.2f und max %d Bildchen kaufen.\n", min,\
(double)avg/(double)anz, max);
exit(0);
}
Ingo Melzer, 04. November 1997