Some C programming help!

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

  1. Mr Coffee

    Mr Coffee New Member

    Joined:
    Jun 25, 2006
    Messages:
    11,338
    Likes Received:
    0
    Location:
    London
    man, i know one of you guys you're good with programming and i need some desperate help.
    Like madly desperate help.:wtc:

    It's some more c programming, and for this i'm willing to pay, but i can't pay much so sorry :o
    Just let me know how much you want to charge for it. :hsd:
    The project basically wants me to re-write the code a little bit, but mostly it's doing the comments to explain what each part does. :hs:

    The program i need to re-do is basically this

    Code:
     [B]Program to guess an animal[/B]
    
    
    [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]
    
     
  2. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    I'll do it for $500.
     
  3. CodeX

    CodeX Guest

    Whoever gave you that example... has no idea what they are doing. That is close to the worst implementation of that that I think anyone could come up with. I really hope it was done that way only to demonstrate specific things... (I really cannot stand when people use block comments for single lines, it pisses me off so much, it makes it impossible to use any actual block comments)

    I'm leaving for a 22 hour drive tomorrow, while my girlfriend is driving I may open up the laptop and write something up for you, no promises though

    Basically though you need it done in the same way with just minor inconsequential changes? I'm not sure I understand the assignment if you are given the entire code...
     
    Last edited by a moderator: Nov 24, 2008
  4. Mr Coffee

    Mr Coffee New Member

    Joined:
    Jun 25, 2006
    Messages:
    11,338
    Likes Received:
    0
    Location:
    London
    Yeah, basically the same thing but rewritten slighly differenlty and with different comments.
     
  5. Mr Coffee

    Mr Coffee New Member

    Joined:
    Jun 25, 2006
    Messages:
    11,338
    Likes Received:
    0
    Location:
    London
    *cough*
     
  6. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    Whats the matter... homework due soon?
     
  7. Mr Coffee

    Mr Coffee New Member

    Joined:
    Jun 25, 2006
    Messages:
    11,338
    Likes Received:
    0
    Location:
    London
    Yesterday, and i've not done it yet. So any help would be appreciated :o
     
  8. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    Yeah... we don't do homework here. You're SOL.
     
  9. johan

    johan Active Member

    Joined:
    Nov 4, 2003
    Messages:
    5,123
    Likes Received:
    0
    Location:
    Sahasrara; magnetic violet infinite
    ba-DUM! and without lube either.
     
  10. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    God forbid he have to learn, ya know?
     

Share This Page