C coding help

Discussion in 'OT Technology' started by Mr Coffee, Nov 5, 2008.

  1. Mr Coffee

    Mr Coffee New Member

    Joined:
    Jun 25, 2006
    Messages:
    11,338
    Likes Received:
    0
    Location:
    London
    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:
    [FONT=monospace][LIST=1]
    [*]   /* This tells the compiler that the program    will use source code defined in the standard C library. */
    [*]   #include <stdio.h>
    [*]   #include <string.h>
    [*]   /* These are convenient  macros.
    [*]      The compiler replaces all    instances of the macro name (all capitals) with the corresponding values    */
    [*]   #define MINIMUM_NUMBER  1
    [*]   #define MAXIMUM_NUMBER  50
    [*]   #define CONTINUE_KEY       "g"
    [*]   #define YES_KEY            "y"
    [*]   #define HIGH_KEY           "+"
    [*]   #define LOW_KEY            "-"
    [*]   #define BUFFER_SIZE        32
    [*]   /* Start of the C program */
    [*]   int main( void )
    [*]   {
    [*]     int guess = 0;
    [*]     int counter = 0;
    [*]     int minimum_value =    MINIMUM_NUMBER;
    [*]     int maximum_value =    MAXIMUM_NUMBER + 1;
    [*]     char buffer[ BUFFER_SIZE ];
    [*]     do
    [*]     {
    [*]       [URL="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"]printf[/URL]( "\n" );
    [*]       [URL="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"]printf[/URL]( "Pick a    number between %d and %d. ", MINIMUM_NUMBER, MAXIMUM_NUMBER );
    [*]       [URL="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"]printf[/URL]( "Press    '%s' when you're ready and I will try to guess your number : ",    CONTINUE_KEY );
    [*]       scanf( "%s", buffer );
    [*]     } while    ( strncmp( buffer, CONTINUE_KEY,    1 ) != 0    );
    [*]     do
    [*]     {
    [*]       counter++;
    [*]       guess = minimum_value + (    ( maximum_value - minimum_value ) / 2 );
    [*]       printf( "\n" );
    [*]       [URL="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"]printf[/URL]( "Attempt    %d: Is your number %d? ", counter, guess );
    [*]       [URL="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"]printf[/URL]( "Type '%s'    if this guess is correct, '%s' if too high or '%s' if too low : ",    YES_KEY, HIGH_KEY, LOW_KEY );
    [*]       scanf( "%s", buffer );
    [*]       if ( strncmp( buffer, YES_KEY, 1 ) == 0 )
    [*]       {
    [*]         [URL="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"]printf[/URL]( "\n" );
    [*]         [URL="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"]printf[/URL]( "It took    %d attempts to guess your number, %d!", counter, guess );
    [*]         [URL="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"]printf[/URL]( "\n" );
    [*]         break;
    [*]       }
    [*]       if ( strncmp( buffer, HIGH_KEY, 1 ) == 0 )
    [*]         maximum_value = guess;
    [*]       else
    [*]         minimum_value = guess;
    [*]     } while    ( 1 );
    [*]     /* Unix convention is to return zero    from main for normal program exit */
    [*]     return 0;
    [*]   }[/LIST][/FONT]
    
    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. Basic or detailed answers are welcome :hsd:

    plzdontmovetotutoringkthnxbye
     
  2. White Stormy

    White Stormy Take that, subspace!

    Joined:
    Sep 17, 2002
    Messages:
    85,486
    Likes Received:
    69
    Location:
    Sparkopolis
  3. Limp_Brisket

    Limp_Brisket New Member

    Joined:
    Jan 2, 2006
    Messages:
    48,422
    Likes Received:
    0
    Location:
    Utah
    so you just always guess the midpoint between high and low? that works, i guess you could use a random number generator to make it a little more guess like.

    my brother made one back in jr high and if you told it that it was too high or too low when it wasn't the computer would say "you're cheating, im not playing with you anymore" and then you couldn't play anymore until you selected the option "apologize to computer". lol
     
  4. CodeX

    CodeX Guest

    You have declared a variable named seed, which I can only assume is intended to seed the random number generator, yet you do not use it instead you call srand() with the first number input by the user. You should seed the random number generator with the system time.

    If you really want to impress your teacher you should do a simple binary search to find the number the user inputs. All you need to do is split the range in half, and ask the user if the number is higher or lower, you then split that half in half, choose the mid value, and again ask if the value is higher or lower... and so on until you find it. This is the most efficient search algorithm provided you have a sorted list, with a runtime of O(log n).

    Actually, if you REALLY wanted to impress your teacher you could implement this recursively in about 5 lines of code. If you are interested in something like that I would help you but I just got done writing code for 8 hours so I can barely even bring myself to type this right now lol
     
    Last edited by a moderator: Nov 5, 2008
  5. CodeX

    CodeX Guest

    ...and you call me worthless. Good show.
     
  6. Mr Coffee

    Mr Coffee New Member

    Joined:
    Jun 25, 2006
    Messages:
    11,338
    Likes Received:
    0
    Location:
    London
    Yeah, i thought it was probably better with system time, but i've not included that in my code... yet :hs:

    I really do want to impress my teacher :o But the thing is that unlike the first code the one i'm assigned to do will have any user input. The search algorithm i figured out early was choosing the mid range number.

    But since i'm shitty with maths and programming in general, i'm not sure if i would count the amount of numbers between the two (example; 2 and 8), there is a difference of 6 digits, so i'll get, what, 5? Then higher or lower and showing the mid-range number, no?

    I'll be working with several other stuff all night long anyway, so if you're still willing to help, shoot me a pm.
     
  7. wabash9000

    wabash9000 Active Member

    Joined:
    Dec 27, 2006
    Messages:
    14,392
    Likes Received:
    0
    Location:
    Overland Park, KS
    have it so that the user has to specify if the number is higher or lower than the guess then reset the high and low bounds and then have it reguess inbetween those values.

    the guess can either be
    pick a random number within the bounds
    or
    pick the number that is halfway inbetween the bounds.
     
  8. CodeX

    CodeX Guest

    pseudo code for a recursive binary search algorithm.

    Code:
    search(min, max) 
    {
       if(max <= min) return not_found
    
       guess = (max-min)/2
    
       //Guess becomes new max, new guess is (new max - min)/2
       if(guess > value) return search(min, guess)
       
       /Guess becomes new min, new guess is (max - new min)/2
       else if(guess < value) return search(guess, max)
       
       //Return guaranteed correct guess
       return guess
    }
    
    You would use it like this:

    Code:
    answer = search(0, 100);
    
    Of course, this assumes the value that you are searching for is known, hence the comparison with the "value" psuedo-variable above. This algorithm is typically used to find the position of a known value in a sorted set of indeterminate values. For your purpose you may have to prompt this information from the user, by simply asking them if the current guess is higher or lower than the actual value.

    Given a set with one million elements this algorithm is guaranteed to find the correct answer within 20 iterations, typically 19.
    If you notice, this function calls itself, nesting each call deeper in the stack until the correct value is found, at which point each nested iteration returns normally and the stack is back to where it started. This is called tail recursion if you are interested in learning more about it
     
    Last edited by a moderator: Nov 6, 2008
  9. Mr Coffee

    Mr Coffee New Member

    Joined:
    Jun 25, 2006
    Messages:
    11,338
    Likes Received:
    0
    Location:
    London
    Okay, gotcha.
    My question is, would i define "guess" in the beginning of the program?
    Also, wouldn't the "value" be used only after the first time i've attempted to guess the correct number?
    It would take the mid-range number and that would be "value" or am i barking up the wrong tree here?
     
  10. CodeX

    CodeX Guest

    The variable guess could be local to the search function, or it could be global, either way it would not need initialization. As you can see as soon as you call search it will be set to the half way point, if you pass 0 and 100 to the function as the min and max then guess would automatically be set to 50 for the first iteration.

    No, value is the actual number, the one you are trying to find. Like I said this would be a non-common use for this algorithm.... as you don't know what value is, but its a guessing game so I am assuming you can just ask the user if your guess is higher or lower than the value, if you can at least do that than this is the most efficient way to find the right number.

    If you can't ask the user that... then hell you might as well guess a random number each time, storing each of your wrong guesses so that you don't do the same one twice
     
  11. Mr Coffee

    Mr Coffee New Member

    Joined:
    Jun 25, 2006
    Messages:
    11,338
    Likes Received:
    0
    Location:
    London
    Ah, but i am asking the user if it's higher or lower.

    So far i've got;

    Code:
    include <stdio.h> 
    include <string.h>
    
    define MINIMUM 1 
    define MAXIMUM 50
    
    define CONTINUE    "c" 
    define HIGHER    "h"
    define LOWER    "l"
    
    define BUFFER_SIZE    32
    
    int main ( void ) //start main program
    
    {
    
    int guess = 0;
    int counter = 0;
    int minimal_value = MINIMUM
    int maximum_value = MAXIMUM +1;
    char buffer [ BUFFER_SIZE ];
    
    do
    
    {
    
    printf( "\n" );
    printf( "Pick a number between %d and %d.", MINIMUM, MAXIMUM _; 
    printf( "Enter '%s' when you're ready and i will attempt to guess your number:", CONTINUE 
    scanf("%s", buffer ); 
    } while 
    
    
    answer = search (MINIMUM, MAXIMUM)
    {
    if (MAXIMUM <= MINIMUM) return not_found (not found hasn't been defined yet)
    
    guess = (MAXIMUM-MINIMUM)/2
    
    The guess is the new max, new guess is (new max - min)/2
    if (guess > value*) return search (MINIMUM, guess)
    
    Same as previous, guess is new max, new guess is (new max - min)/2
    else if (guess < value) return search (guess, MAXIMUM)
    
    Return guess
    
    }
    
    
    I suppose my next action is to "printf( "Is '%s' your number?", guess;"
    And then if the user enters lower or higher;
    if Higher return search (guess/maximum)

    Or something along that line?
     
  12. CodeX

    CodeX Guest

    You still need help with this? Sorry I didn't get back to you last night, but now that its friday ill write the damn thing for you if you want lol
     
  13. Mr Coffee

    Mr Coffee New Member

    Joined:
    Jun 25, 2006
    Messages:
    11,338
    Likes Received:
    0
    Location:
    London
    Hehehehe, nah, i finally sorted it out and got it up and running :bowdown::bowdown:

    But if you still want to help out, i am supposed to hand in a work a week from now that is similar :mamoru:

    Code:
    [FONT=monospace] [LIST=1]
    [*]   #include <stdio.h>
    [*]   #include <string.h>
    [*]   #include <time.h>
    [*]    
    [*]   /*
    [*]   * List of animals that the user can select    for the game.
    [*]   * Below the animal names are    characteristics for each animal listed in the same order.
    [*]   * Each entry should be separated by a space    and spelled in all lowercase.
    [*]   */
    [*]   #define ANIMALS_LIST         "giraffe trout tiger elephant jellyfish eagle piranha hummingbird parrot    dolphin"
    [*]   #define JUNGLE_ANIMALS    "0 1 1 1 0    0 1 0 1 0"
    [*]   #define HERBIVORES           "1 1 0 1 1 0 0 1 0 0"
    [*]   #define SWIMMING_ANIMALS  "0 1 0 0 1 0 1 0    0 1"
    [*]   #define FLYING_ANIMALS    "0 0 0 0 0    1 0 1 1 0"
    [*]    
    [*]   /*
    [*]   * Each entry in the list is separated by a    space.
    [*]   */
    [*]   #define ANIMAL_DELIMITER  " "
    [*]    
    [*]   /*
    [*]   * The total number of animals determines    the amount of memory used by the program.
    [*]   */
    [*]   #define TOTAL_ANIMALS        10
    [*]    
    [*]   /*
    [*]   * Wait for user to type this before    continuing with the game.
    [*]   */
    [*]   #define CONTINUE_KEY         "g"
    [*]    
    [*]   /*
    [*]   * The user must type this to indicate a yes    answer.
    [*]   */
    [*]   #define YES_KEY              "y"
    [*]    
    [*]   /*
    [*]   * The user should type this to indicate a    no answer.
    [*]   * But, any answer other than yes could be    considered to be a no answer.
    [*]   */
    [*]   #define NO_KEY               "n"
    [*]    
    [*]   /*
    [*]   * Amount of characters for the user to type    their answers.
    [*]   */
    [*]   #define BUFFER_SIZE          32
    [*]    
    [*]   /*
    [*]   * Turn off debugging output by setting this    to zero.
    [*]   * Any non-zero value will turn debugging    on.
    [*]   */
    [*]   #define DEBUG_STATUS         1
    [*]    
    [*]    
    [*]   /*
    [*]   * Global array to store all the animals in    the list.
    [*]   */
    [*]   char* animals_list[ TOTAL_ANIMALS ] = { NULL };
    [*]    
    [*]   /*
    [*]   * Global array to store animals which live    in the jungle (using the same index order).
    [*]   */
    [*]   int   jungle_animals[ TOTAL_ANIMALS ] = { 0 };
    [*]    
    [*]   /*
    [*]   * Global array to store animals which are    herbivores (using the same index order).
    [*]   */
    [*]   int   herbivores[ TOTAL_ANIMALS ] = { 0 };
    [*]    
    [*]   /*
    [*]   * Global array to store animals which swim    (using the same index order).
    [*]   */
    [*]   int   swimming_animals[ TOTAL_ANIMALS ] = { 0 };
    [*]    
    [*]   /*
    [*]   * Global array to store animals which fly    (using the same index order).
    [*]   */
    [*]   int   flying_animals[ TOTAL_ANIMALS ] = { 0 };
    [*]    
    [*]    
    [*]   /*
    [*]   * Please see function definitions for    description of function prototypes.
    [*]   */
    [*]   void  setupGameData();
    [*]   void  startGame();
    [*]   void  askUserQuestion( int, char*,    int* );
    [*]   void  displayRemainingAnimals();
    [*]   int      countRemainingAnimals();
    [*]   char* findLastAnimal();
    [*]    
    [*]    
    [*]   /*
    [*]   * Start of the program.
    [*]   */
    [*]   int main( void )
    [*]   {
    [*]     /* Variable to indicate that the    game should continue */
    [*]     int continue_game = 1;
    [*]    
    [*]     /* Initialize the number of attempts    to zero */
    [*]     int guesses = 0;
    [*]    
    [*]     time_t start_time, end_time;
    [*]    
    [*]     setupGameData();
    [*]    
    [*]     startGame();
    [*]    
    [*]     /* Start timing the game session    */
    [*]     start_time = time( NULL );
    [*]     [URL="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"]printf[/URL]( "\nGame started at %s\n",    ctime( &start_time ) );
    [*]    
    [*]     /* Start the main loop to play the    game */
    [*]     do
    [*]     {
    [*]       /* Increment the attempts    counter by 1 */
    [*]       guesses++;
    [*]    
    [*]       switch ( guesses )
    [*]       {
    [*]         case 1:
    [*]           askUserQuestion( guesses, "\nAttempt    %d: Does your animal live in the jungle? ", jungle_animals );
    [*]           break;
    [*]         case 2:
    [*]           askUserQuestion( guesses, "\nAttempt    %d: Does your animal eat only plants? ", herbivores );
    [*]           break;
    [*]         case 3:
    [*]           askUserQuestion( guesses, "\nAttempt    %d: Does your animal swim? ", swimming_animals );
    [*]           break;
    [*]         case 4:
    [*]           askUserQuestion( guesses, "\nAttempt    %d: Does your animal fly? ", flying_animals );
    [*]           break;
    [*]         default  :
    [*]           /* Losing end    of game condition */
    [*]           [URL="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"]printf[/URL]( "\nYou win, I'm out of ideas!\n"    );
    [*]    
    [*]           /* Stop the    game */
    [*]           continue_game = 0;
    [*]           break;
    [*]       }
    [*]    
    [*]       if ( DEBUG_STATUS )
    [*]         displayRemainingAnimals();
    [*]    
    [*]       /* Winning end of game    condition */
    [*]       if ( countRemainingAnimals() == 1 )
    [*]       {
    [*]         [URL="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"]printf[/URL]( "\nIt took %d attempts to guess your animal, %s!\n", guesses, findLastAnimal() );
    [*]    
    [*]         /* Stop the game    */
    [*]         continue_game = 0;
    [*]       }
    [*]    
    [*]     } while    ( continue_game );
    [*]    
    [*]     /* Stop timing the game session    */
    [*]     end_time = time( NULL );
    [*]     [URL="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"]printf[/URL]( "\nGame ended at %s\n",    ctime( &end_time ) );
    [*]    
    [*]     /* Unix convention is to return zero    from main for normal program exit */
    [*]     return 0;
    [*]   }
    [*]    
    [*]    
    [*]   /*
    [*]   * setupGameData : Parses the list of    animals and characteristics.
    [*]   */
    [*]   void setupGameData()
    [*]   {
    [*]     char* animal;
    [*]     int   index;
    [*]    
    [*]     /* Start the process to parse the    list of animals and characteristics */
    [*]     animal = strtok( strdup( ANIMALS_LIST ), ANIMAL_DELIMITER    );
    [*]    
    [*]     /* Go through the list of animals    until the end of the list is found, indicated by a NULL value */
    [*]     for (    index = 0; ( index <    TOTAL_ANIMALS ) && (    animal != NULL ); index++ )
    [*]     {
    [*]       /* Assign the individual    animal into the appropriate index */
    [*]       animals_list[ index ] = animal;
    [*]    
    [*]       /* Continue to the next    animal */
    [*]       animal = strtok( NULL, ANIMAL_DELIMITER );
    [*]     }
    [*]    
    [*]     /* Update characteristic for animals    that live in the jungle */
    [*]     animal = strtok( strdup( JUNGLE_ANIMALS ), ANIMAL_DELIMITER    );
    [*]     for (    index = 0; ( index <    TOTAL_ANIMALS ) && (    animal != NULL ); index++ )
    [*]     {
    [*]       jungle_animals[ index    ] = atoi( animal );
    [*]       animal = strtok( NULL, ANIMAL_DELIMITER );
    [*]     }
    [*]    
    [*]     /* Update characteristic for    herbivores */
    [*]     animal = strtok( strdup( HERBIVORES ), ANIMAL_DELIMITER );
    [*]     for (    index = 0; ( index <    TOTAL_ANIMALS ) && (    animal != NULL ); index++ )
    [*]     {
    [*]       herbivores[ index ] = atoi( animal );
    [*]       animal = strtok( NULL, ANIMAL_DELIMITER );
    [*]     }
    [*]    
    [*]     /* Update charactertistic for    swimming animals */
    [*]     animal = strtok( strdup( SWIMMING_ANIMALS ), ANIMAL_DELIMITER    );
    [*]     for (    index = 0; ( index <    TOTAL_ANIMALS ) && (    animal != NULL ); index++ )
    [*]     {
    [*]       swimming_animals[ index    ] = atoi( animal );
    [*]       animal = strtok( NULL, ANIMAL_DELIMITER );
    [*]     }
    [*]    
    [*]     /* Update characteristic for flying    animals */
    [*]     animal = strtok( strdup( FLYING_ANIMALS ), ANIMAL_DELIMITER    );
    [*]     for (    index = 0; ( index <    TOTAL_ANIMALS ) && (    animal != NULL ); index++ )
    [*]     {
    [*]       flying_animals[ index    ] = atoi( animal );
    [*]       animal = strtok( NULL, ANIMAL_DELIMITER );
    [*]     }
    [*]   }
    [*]    
    [*]    
    [*]   /*
    [*]   * startGame : Display game instructions and    list of available animals for the user to select.
    [*]   *                If debug is on, also display the list of animals and    characteristics.
    [*]   */
    [*]   void startGame()
    [*]   {
    [*]     char buffer[ BUFFER_SIZE ];
    [*]     int  index;
    [*]    
    [*]     do
    [*]     {
    [*]       [URL="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"]printf[/URL]( "\nPick one of the following animals:\n\n" );
    [*]       for ( index = 0; index < TOTAL_ANIMALS;    index++ )
    [*]         [URL="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"]printf[/URL]( "%s    ", animals_list[ index ]    );
    [*]       [URL="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"]printf[/URL]( "\n\nPress '%s' when you're ready and I    will try to guess the animal : ", CONTINUE_KEY );
    [*]       scanf( "%s", buffer );
    [*]     } while    ( strncmp( buffer, CONTINUE_KEY,    1 ) != 0    );
    [*]    
    [*]     if (    DEBUG_STATUS )
    [*]     {
    [*]       /* Go through and display the    list of animals and characteristics */
    [*]       for ( index = 0; index < TOTAL_ANIMALS;    index++ )
    [*]         [URL="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"]printf[/URL]( "%s: %d %d    %d %d \n", animals_list[    index ], jungle_animals[ index    ], herbivores[ index ], swimming_animals[ index ], flying_animals[ index ] );
    [*]     }
    [*]   }
    [*]    
    [*]    
    [*]   /*
    [*]   * askUserQuestion : Ask the user questions    about the animal
    [*]   *
    [*]   * Parameters      : Number    of current attempt
    [*]   *                      Text of the question
    [*]   *                      Array of data for that question
    [*]   */
    [*]   void askUserQuestion( int guessNumber, char* question, int* animalData )
    [*]   {
    [*]     char buffer[ BUFFER_SIZE ];
    [*]     int  index;
    [*]    
    [*]     [URL="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"]printf[/URL]( question, guessNumber    );
    [*]     [URL="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"]printf[/URL]( "Type '%s'    if yes '%s' if no : ", YES_KEY, NO_KEY );
    [*]    
    [*]     /* Wait for the user answer    */
    [*]     scanf( "%s", buffer );
    [*]    
    [*]     /* Go through the list of remaining    animals and take action based on the user answer */
    [*]     for (    index = 0; index < TOTAL_ANIMALS; index++ )
    [*]     {
    [*]       /* Only process if there is    an animal at that location in the array */
    [*]       if ( animals_list[ index ] != NULL )
    [*]       {
    [*]         if ( strncmp( buffer, YES_KEY, 1 ) == 0 )
    [*]         {
    [*]           /* If the user    answer is yes, then remove animals do not match */
    [*]           if ( animalData[ index ] != 1 )
    [*]             animals_list[ index ] = NULL;
    [*]         }
    [*]         else
    [*]         {
    [*]           /* If the user    answer is no, then remove animals which match */
    [*]           if ( animalData[ index ] == 1 )
    [*]             animals_list[ index ] = NULL;
    [*]         }
    [*]       }
    [*]     }
    [*]   }
    [*]    
    [*]    
    [*]   /*
    [*]   * displayRemainingAnimals : Display the    list of possible animals remaining.
    [*]   */
    [*]   void displayRemainingAnimals()
    [*]   {
    [*]     int index;
    [*]    
    [*]     [URL="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"]printf[/URL]( "\nAnimals remaining: " );
    [*]     for (    index = 0; index < TOTAL_ANIMALS; index++ )
    [*]     {
    [*]       if ( animals_list[ index ] != NULL )
    [*]         [URL="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"]printf[/URL]( "%s    ", animals_list[ index ]    );
    [*]     }
    [*]     [URL="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"]printf[/URL]( "\n" );
    [*]   }
    [*]    
    [*]    
    [*]   /*
    [*]   * countRemainingAnimals : Count the list of    possible animals remaining.
    [*]   *
    [*]   * Returns the number counted.
    [*]   */
    [*]   int countRemainingAnimals()
    [*]   {
    [*]     int animals_remaining = 0;
    [*]     int index;
    [*]    
    [*]     for (    index = 0; index < TOTAL_ANIMALS; index++ )
    [*]     {
    [*]       if ( animals_list[ index ] != NULL )
    [*]         animals_remaining++;
    [*]     }
    [*]    
    [*]     return animals_remaining;
    [*]   }
    [*]    
    [*]    
    [*]   /*
    [*]   * findLastAnimal : Find the last animal on    the list.
    [*]   *
    [*]   * Returns the animal found.
    [*]   */
    [*]   char* findLastAnimal()
    [*]   {
    [*]     int index;
    [*]    
    [*]     for (    index = 0; index < TOTAL_ANIMALS; index++ )
    [*]     {
    [*]       if ( animals_list[ index ] != NULL )
    [*]         return    animals_list[ index ];
    [*]     }
    [*]    
    [*]     return NULL;
    [*]   }
    [/LIST][/FONT]
    
    Yeah, we're supposed to make a program to guess an animal, but this is a bit more complex than the previous one :o
     

Share This Page