numbering game: guess a value
authorMarkus Bröker <mbroeker@largo.dyndns.tv>
Thu, 16 Apr 2009 12:51:16 +0200
changeset 86 86b83a979d9e
parent 85 9568a180fc43
child 87 b2f1756c17ca
numbering game: guess a value It was a short contest in IRC. Write a numbering game. Anyway, i have lost: The winner realized it in 12 lines. committer: Markus Bröker <mbroeker@largo.homelinux.org>
Makefile
numbers.c
--- a/Makefile
+++ b/Makefile
@@ -64,6 +64,7 @@
 TARGET += fts
 TARGET += clplaner
 TARGET += daemon
+TARGET += numbers
 
 .SUFFIXES: .c .cc .asm
 
@@ -307,6 +308,10 @@
 	@echo Linking $<...
 	@$(CC) -Wall -O2 -g -ggdb $< -o $@
 
+numbers: numbers.o
+	@echo Linking $<...
+	@$(CC) -Wall -O2 -g -ggdb $< -o $@
+
 .PHONY: beauty clean uninstall
 
 clean:
new file mode 100644
--- /dev/null
+++ b/numbers.c
@@ -0,0 +1,22 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#define getrandom(max) (1+(int)((float)(max)*rand()/RAND_MAX+1.0))
+
+int main (void)
+{
+    while (1) {
+        int guess = -1, value = getrandom (100), count = 0;
+        do {
+            printf ("Enter a number: ");
+            if (scanf ("%d", &guess) < 0)
+                (void)getchar ();
+            else
+                count++;
+            if (guess != value)
+                printf ("X is %s than %d\n", (value > guess) ? "higher" : "lower", guess);
+        } while (value != guess);
+        printf ("SUCCESS: You got it after %d attempts\n", count);
+    }
+    return EXIT_SUCCESS;
+}