numbers.c
author Markus Bröker <mbroeker@largo.dyndns.tv>
Fri, 27 Dec 2013 16:00:09 +0100
changeset 167 7c6b221900bf
parent 140 05d42a3737a4
permissions -rw-r--r--
Proper Datatypes to leave the 80's
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
140
05d42a3737a4 Comments corrected: misspellings and other trivial things
Markus Bröker <mbroeker@volpe.spectre.org>
parents: 86
diff changeset
     1
/**
05d42a3737a4 Comments corrected: misspellings and other trivial things
Markus Bröker <mbroeker@volpe.spectre.org>
parents: 86
diff changeset
     2
 * numbers.c
05d42a3737a4 Comments corrected: misspellings and other trivial things
Markus Bröker <mbroeker@volpe.spectre.org>
parents: 86
diff changeset
     3
 * Copyright (C) 2008 Markus Broeker
05d42a3737a4 Comments corrected: misspellings and other trivial things
Markus Bröker <mbroeker@volpe.spectre.org>
parents: 86
diff changeset
     4
 */
05d42a3737a4 Comments corrected: misspellings and other trivial things
Markus Bröker <mbroeker@volpe.spectre.org>
parents: 86
diff changeset
     5
86
86b83a979d9e numbering game: guess a value
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     6
#include <stdio.h>
86b83a979d9e numbering game: guess a value
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     7
#include <stdlib.h>
86b83a979d9e numbering game: guess a value
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     8
86b83a979d9e numbering game: guess a value
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
     9
#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
    10
86b83a979d9e numbering game: guess a value
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    11
int main (void)
86b83a979d9e numbering game: guess a value
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    12
{
86b83a979d9e numbering game: guess a value
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    13
    while (1) {
86b83a979d9e numbering game: guess a value
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    14
        int guess = -1, value = getrandom (100), count = 0;
86b83a979d9e numbering game: guess a value
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    15
        do {
86b83a979d9e numbering game: guess a value
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    16
            printf ("Enter a number: ");
86b83a979d9e numbering game: guess a value
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    17
            if (scanf ("%d", &guess) < 0)
86b83a979d9e numbering game: guess a value
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    18
                (void)getchar ();
86b83a979d9e numbering game: guess a value
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    19
            else
86b83a979d9e numbering game: guess a value
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    20
                count++;
86b83a979d9e numbering game: guess a value
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    21
            if (guess != value)
86b83a979d9e numbering game: guess a value
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    22
                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
    23
        } while (value != guess);
86b83a979d9e numbering game: guess a value
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    24
        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
    25
    }
86b83a979d9e numbering game: guess a value
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    26
    return EXIT_SUCCESS;
86b83a979d9e numbering game: guess a value
Markus Bröker <mbroeker@largo.dyndns.tv>
parents:
diff changeset
    27
}