dcsimg


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: C coding help (random number guessing)

  1. #1
    Join Date
    Nov 2008
    Posts
    1

    C coding help (random number guessing)

    Right, i'm not asking you to do my coursework for me, but i could use some help/pointers of what i should do.

    I'm supposed to do a c game where the user chooses a number between 1-50 (as an example, i can do 100 or 500) and the program is supposed to guess.
    Now i've got the basics of c, and i've found a code that's helped me understand how to a similar program, where the user guesses it, but i could use some help.

    The following is the code where the user guesses it;
    Code:
    #include <stdio.h>
     #include <stdlib.h>
     
     #include <string.h>
     
     
     int main()
     {
         int guess;
         int num = 0;
         int answer;
         int i;
         unsigned seed;
        
         printf("Enter guess:\n");
         scanf("%d",&guess);
     
         srand(guess);
     
             for(i=1;i<=1000;i++); {
     
         answer = rand() % 100 + 1;
     
         while ( guess != answer ) {
             num++;
             printf("Enter guess %d: ",num);
            scanf("%d",&guess);
            
             if ( guess > answer )
                 printf("Too high,try again\n");
             if ( guess < answer )
                 printf("Too low,try again\n");
         }
         printf("Correct! you guessed the number!\n\n",num);
         }
        
         return 0;
     }
    I'm sure i can turn it around fairly easily to become reversed roles.
    But this is the example we were given for our work;

    Code:
       1.   /* This tells the compiler that the program    will use source code defined in the standard C library. */
       2.   #include <stdio.h>
       3.   #include <string.h>
       4.   /* These are convenient  macros.
       5.      The compiler replaces all    instances of the macro name (all capitals) with the corresponding values    */
       6.   #define MINIMUM_NUMBER  1
       7.   #define MAXIMUM_NUMBER  50
       8.   #define CONTINUE_KEY       "g"
       9.   #define YES_KEY            "y"
      10.   #define HIGH_KEY           "+"
      11.   #define LOW_KEY            "-"
      12.   #define BUFFER_SIZE        32
      13.   /* Start of the C program */
      14.   int main( void )
      15.   {
      16.     int guess = 0;
      17.     int counter = 0;
      18.     int minimum_value =    MINIMUM_NUMBER;
      19.     int maximum_value =    MAXIMUM_NUMBER + 1;
      20.     char buffer[ BUFFER_SIZE ];
      21.     do
      22.     {
      23.       printf( "\n" );
      24.       printf( "Pick a    number between %d and %d. ", MINIMUM_NUMBER, MAXIMUM_NUMBER );
      25.       printf( "Press    '%s' when you're ready and I will try to guess your number : ",    CONTINUE_KEY );
      26.       scanf( "%s", buffer );
      27.     } while    ( strncmp( buffer, CONTINUE_KEY,    1 ) != 0    );
      28.     do
      29.     {
      30.       counter++;
      31.       guess = minimum_value + (    ( maximum_value - minimum_value ) / 2 );
      32.       printf( "\n" );
      33.       printf( "Attempt    %d: Is your number %d? ", counter, guess );
      34.       printf( "Type '%s'    if this guess is correct, '%s' if too high or '%s' if too low : ",    YES_KEY, HIGH_KEY, LOW_KEY );
      35.       scanf( "%s", buffer );
      36.       if ( strncmp( buffer, YES_KEY, 1 ) == 0 )
      37.       {
      38.         printf( "\n" );
      39.         printf( "It took    %d attempts to guess your number, %d!", counter, guess );
      40.         printf( "\n" );
      41.         break;
      42.       }
      43.       if ( strncmp( buffer, HIGH_KEY, 1 ) == 0 )
      44.         maximum_value = guess;
      45.       else
      46.         minimum_value = guess;
      47.     } while    ( 1 );
      48.     /* Unix convention is to return zero    from main for normal program exit */
      49.     return 0;
      50.   }
    As you probably noticed, it's supposed to be a sort of userinterface, which i can do by myself.

    But what i want to know is how can i, in a different way than the second example, guess a random number and then display it. Basic or detailed answers are very much welcome and i'd greatly appreciate any help i could get.

  2. #2
    Join Date
    Dec 2003
    Posts
    3,366
    a computer would probably do a binary search. so if you pick a number between 1 and 50, and that number is ... 13 for example, you get something like this:

    25? no, lower.
    12? no, higher
    18? no, lower
    15? no, lower
    13? got it.
    the idea is to have a low and high that you know the value is between, and go 1/2 way each time, and use the result as a new high or low depending on the response. Eventually, you will get the right value.

Similar Threads

  1. Random Number Selection For Game
    By gordon87 in forum C++
    Replies: 2
    Last Post: 12-07-2007, 06:17 PM
  2. Replies: 0
    Last Post: 07-05-2007, 11:12 AM
  3. Random number help??
    By vbjunkie1 in forum C++
    Replies: 5
    Last Post: 03-24-2007, 03:13 AM
  4. Replies: 18
    Last Post: 12-13-2006, 03:49 PM
  5. Help with Random number and text Box
    By cmvw in forum VB Classic
    Replies: 7
    Last Post: 06-28-2006, 10:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center