author | Markus Bröker <mbroeker@largo.dyndns.tv> |
Tue, 09 Feb 2010 08:38:01 +0100 | |
changeset 109 | 3d84eda3f16f |
parent 86 | 86b83a979d9e |
child 140 | 05d42a3737a4 |
permissions | -rw-r--r-- |
86
86b83a979d9e
numbering game: guess a value
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
1 |
#include <stdio.h> |
86b83a979d9e
numbering game: guess a value
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
2 |
#include <stdlib.h> |
86b83a979d9e
numbering game: guess a value
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
3 |
|
86b83a979d9e
numbering game: guess a value
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
4 |
#define getrandom(max) (1+(int)((float)(max)*rand()/RAND_MAX+1.0)) |
86b83a979d9e
numbering game: guess a value
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
5 |
|
86b83a979d9e
numbering game: guess a value
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
6 |
int main (void) |
86b83a979d9e
numbering game: guess a value
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
7 |
{ |
86b83a979d9e
numbering game: guess a value
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
8 |
while (1) { |
86b83a979d9e
numbering game: guess a value
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
9 |
int guess = -1, value = getrandom (100), count = 0; |
86b83a979d9e
numbering game: guess a value
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
10 |
do { |
86b83a979d9e
numbering game: guess a value
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
11 |
printf ("Enter a number: "); |
86b83a979d9e
numbering game: guess a value
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
12 |
if (scanf ("%d", &guess) < 0) |
86b83a979d9e
numbering game: guess a value
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
13 |
(void)getchar (); |
86b83a979d9e
numbering game: guess a value
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
14 |
else |
86b83a979d9e
numbering game: guess a value
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
15 |
count++; |
86b83a979d9e
numbering game: guess a value
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
16 |
if (guess != value) |
86b83a979d9e
numbering game: guess a value
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
17 |
printf ("X is %s than %d\n", (value > guess) ? "higher" : "lower", guess); |
86b83a979d9e
numbering game: guess a value
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
18 |
} while (value != guess); |
86b83a979d9e
numbering game: guess a value
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
19 |
printf ("SUCCESS: You got it after %d attempts\n", count); |
86b83a979d9e
numbering game: guess a value
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
20 |
} |
86b83a979d9e
numbering game: guess a value
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
21 |
return EXIT_SUCCESS; |
86b83a979d9e
numbering game: guess a value
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff
changeset
|
22 |
} |