UMBC CMSC 201
Fall '08

UMBC | CSEE | 201 | 201 F'08 | lectures | projects | help

Separate Compilation Practice Problem

As in previous exercises, you may click on the problem name to see the answer. You can then click on Explain for that answer to see a complete explanation.

  1. Take the contents of the file bigcards.c shown below and break it up into several files, where each file groups functions for their similarities or reuse.

    You should have three .c files : and two header file (.h files) :

    The idea of keeping util.c and util.h files is that you can add modules to the files as you write them as part of upcoming projects. This will become a repository of useful utility functions. You should never remove a function from the utility file. Leave all the functions in there even if you aren't calling them all in your current project.

    
    /*************************************************************************\
    * Filename:      bigcards.c                                               *
    * Author:        Sue Evans                                                *
    * Date Written:  9/19/00                                                  *
    * Modified:      9/18/08                                                  *
    * Section #:     02XX                                                     *
    * Email:         bogar@cs.umbc.edu                                        *
    *                                                                         *
    * This program will simulate the drawing of a card randomly from a deck   *
    * of 52 cards, ask the user to guess that card, and report either a win   *
    * or a loss.                                                              *
    \*************************************************************************/
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int  GetValidInt (int min, int max);
    void PrintInstructions (void);
    void SetRandomSeed (void);
    int  GetRandomNumber (int low, int high);
    void PrintCard(int pip, int suit);
    void PrintResults (int pip, int suit, int playerPip, int playerSuit);
    
    #define MINPIP   1
    #define MAXPIP  13
    #define MINSUIT  1
    #define MAXSUIT  4
    
    #define ACE      1
    #define JACK    11
    #define QUEEN   12
    #define KING    13
    
    #define HEARTS   1
    #define SPADES   2
    #define DIAMONDS 3
    #define CLUBS    4
    
    int main ( )
    {
       int pip, suit, playerPip, playerSuit;
    
       /*seed the random number generator*/
       SetRandomSeed( );
    
       /*computer draws a card randomly*/
       pip = GetRandomNumber (MINPIP, MAXPIP);
       suit = GetRandomNumber (MINSUIT, MAXSUIT);
    
       /*greet user and get the user's card*/
       PrintInstructions( );
       printf("Pick the face value of your card.\n");
       playerPip = GetValidInt (MINPIP, MAXPIP);
       printf("Pick the suit of your card.\n");
       playerSuit = GetValidInt (MINSUIT, MAXSUIT);
    
       PrintResults(pip, suit, playerPip, playerSuit);
    
       return 0;
    } 
    
    /*********************************************
    ** Function: GetValidInt()
    ** GetValidInt() gets an integer from the user between
    ** specified minimum and maximum values, and will
    ** reprompt the user until it gets one that is valid.
    **                                                  
    ** Inputs: min and max, the minimum and maximum      
    **          (inclusive) values for the entered integer 
    ** Output: returns an integer between min and max,  
    **          inclusively                              
    *********************************************/    
    int GetValidInt(int min, int max)
    {
       /* is set greater than max so the loop will be entered*/
       int input = max + 1;
    
       /* Loop assures valid input */
       while( input < min || input > max )
       {
          printf("Please enter an integer between");
          printf(" %d and %d.\n", min, max);
          scanf("%d", &input);
       }
    
       return input;
    }
    
    
    
    /*********************************************
    ** Function: PrintInstructions()               
    **                                            
    ** PrintInstructions() prints the instructions
    ** for the cards program                           
    **                                            
    ** Inputs: none                                
    ** Output: none (Side-effect: prints to screen)
    *********************************************/    
    void PrintInstructions (void)
    {
       printf("\n\n");
       printf("***************************************\n");
       printf("*This program draws a card randomly   *\n");
       printf("*from a deck of 52 cards.  You should *\n");
       printf("*guess a card by entering a number    *\n");
       printf("*between 1 and 13 that shows the face *\n");
       printf("*value of the card, where Jack is 11, *\n");
       printf("*Queen is 12, and King is 13 and then *\n");
       printf("*Indicate the suit of the card using  *\n");
       printf("*1 through 4, where a 1 is hearts, a  *\n");
       printf("*2 is spades, 3 is diamonds and  4 is *\n");
       printf("*clubs.  A win or a loss is declared. *\n");
       printf("***************************************\n\n");
    }
    
    
    /*********************************************
    ** Function: GetRandomNumber
    ** Usage:    x = GetRandomNumber();
    **
    ** GetRandomNumber() returns a pseudo-random integer
    ** between the values passed into the function: low and 
    ** high, inclusive.
    **
    ** Inputs: low - an int which is the lowest value 
    **         in the range of acceptable random numbers
    **         high - an int which is the highest value
    **         in the range of acceptable random numbers
    ** Output: returns a random integer between low and 
    **         high, inclusive
    *********************************************/
    int GetRandomNumber (int low, int high) 
    {
       int num ;
    
       /* Call rand() to get a large random number. */
       num = rand() ;
    
       /* Scale the random number within range. */
       num = num % (high - low + 1) + low ;
    
       return num ;
    }
    
    
    /*********************************************
    ** Function: SetRandomSeed
    ** Usage:    SetRandomSeed();
    **
    ** The pseudo-random number generator is seeded
    ** with a call to time() assuring a different
    ** seed everytime the program runs.
    **
    ** Inputs: None
    ** Output: No value is returned
    *********************************************/
    void SetRandomSeed (void) 
    {
       int timeSeed ;
    
       /* Use the time function to set the seed. */
       timeSeed = (int) time(0) ;
       srand(timeSeed) ;
    }
    
    
    /*********************************************
    ** Function: PrintCard
    ** Usage:    PrintCard(pip, suit);
    **
    ** Prints the name of the card.
    **
    ** Inputs: pip -  an integer between 1 and 13
    **                 indicating and Ace - King
    **	   suit - an integer between 1 and 4
    **	           indicating the card's suit
    ** Output: None 
    **
    ** Assumptions: Relies on #defines for suits and
    **              Ace, Jack, Queen & King.
    *********************************************/
    void PrintCard(int pip, int suit)
    {
       /*print the pip of the card*/
       switch (pip)
       {
          case ACE: 
    	 printf ("Ace ");
    	 break;
          case JACK:
    	 printf ("Jack ");
    	 break;
          case QUEEN:
    	 printf ("Queen ");
    	 break;
          case KING:
    	 printf ("King ");
    	 break;
          case 2:   
          case 3:   
          case 4:
          case 5:   
          case 6:   
          case 7:
          case 8:   
          case 9:   
          case 10:
    	 printf ("%2d ", pip);
    	 break;
          default: 
    	 printf("%d - Not Valid\n", pip);
    	 break;
       }
    
       /*print the suit of the card*/
       switch (suit)
       {
          case HEARTS: 
    	 printf ("of Hearts\n");
    	 break;
          case SPADES:
    	 printf ("of Spades\n");
    	 break;
          case DIAMONDS:
    	 printf ("of Diamonds\n");
    	 break;
          case CLUBS:
    	 printf ("of Clubs\n");
    	 break;
          default: 
    	 printf("%d - Not Valid\n", suit);
    	 break;
       }
    }
    
    
    /******************************************************************
    ** Function: PrintResults
    ** Usage:    PrintResults(pip, suit, playerPip, playerSuit);
    **
    ** Prints the two cards and the results of the game (win or lose)
    **
    ** Inputs: pip -  an integer between 1 and 13 indicating  
    **                 Ace - King that was drawn by the computer
    **	   suit - an integer between 1 and 4 indicating the
    **	           card's suit that was drawn by the computer
    **         playerPip - an integer between 1 and 13 
    **                      indicating Ace - King chosen by the user
    **	   playerSuit - an integer between 1 and 4
    **	                 indicating the card's suit chosen by the user
    ** Output: None 
    *****************************************************************/
    void PrintResults (int pip, int suit, int playerPip, int playerSuit)
    {
       /*print the cards*/
       printf("\n\nI picked the ");
       PrintCard(pip, suit);
       printf("and you picked the ");
       PrintCard(playerPip, playerSuit);
    
       /*compare the cards and declare winner or loser*/
       if (pip == playerPip && suit == playerSuit)
       {
          printf("\nYou Win !!!\n\n");
       }
       else
       {
          printf("\nSorry, you lose\n\n");
       }
    }
    
    


  2. Compile each .c file separately, creating .o files for each one.
    Then link the .o files together to generate the executable file, a.out


  • separate.c
  • /*************************************************************************\
    * Filename:      separate.c                                               *
    * Author:        Sue Evans                                                *
    * Date Written:  9/17/08                                                  *
    * Section #:     02XX                                                     *
    * Email:         bogar@cs.umbc.edu                                        *
    *                                                                         *
    * This program will simulate the drawing of a card randomly from a deck   *
    * of 52 cards, ask the user to guess that card, and report either a win   *
    * or a loss.                                                              *
    \*************************************************************************/
    
    #include <stdio.h>
    
    #include "cards.h"
    #include "util.h"
    
    
    #define MINPIP   1
    #define MAXPIP  13
    #define MINSUIT  1
    #define MAXSUIT  4
    
    
    int main ( )
    {
       int pip, suit, playerPip, playerSuit;
    
       /*seed the random number generator*/
       SetRandomSeed( );
    
       /*computer draws a card randomly*/
       pip = GetRandomNumber (MINPIP, MAXPIP);
       suit = GetRandomNumber (MINSUIT, MAXSUIT);
    
       /*greet user and get the user's card*/
       PrintInstructions( );
       printf("Pick the face value of your card.\n");
       playerPip = GetValidInt (MINPIP, MAXPIP);
       printf("Pick the suit of your card.\n");
       playerSuit = GetValidInt (MINSUIT, MAXSUIT);
    
       PrintResults(pip, suit, playerPip, playerSuit);
    
       return 0;
    }
     
    
    Explain


  • cards.c
  • 
    /*************************************************************************\
    * Filename:      cards.c                                                  *
    * Author:        Sue Evans                                                *
    * Date Written:  9/17/08                                                  *
    * Section #:     02XX                                                     *
    * Email:         bogar@cs.umbc.edu                                        *
    *                                                                         *
    * This file contains the function definitions that pertain to cards that  *
    * are needed to play the simple card game found in separate.c             *
    *                                                                         *
    * PrintInstructions() prints instructions for the player                  *
    * PrintCards()        prints the pip and suit of a card                   *
    * PrintResults()      prints the cards chosen by the computer and the     *
    *                     player and whether the player won or lost           *
    \*************************************************************************/
    
    #include <stdio.h>
    
    #include "cards.h"
    
    
    #define ACE      1
    #define JACK    11
    #define QUEEN   12
    #define KING    13
    
    #define HEARTS   1
    #define SPADES   2
    #define DIAMONDS 3
    #define CLUBS    4
    
    
    
    /*********************************************
    ** Function: PrintInstructions()               
    **                                            
    ** PrintInstructions() prints the instructions
    ** for the cards program                           
    **                                            
    ** Inputs: none                                
    ** Output: none (Side-effect: prints to screen)
    *********************************************/      
    void PrintInstructions (void)
    {
       printf("\n\n");
       printf("***************************************\n");
       printf("*This program draws a card randomly   *\n");
       printf("*from a deck of 52 cards.  You should *\n");
       printf("*guess a card by entering a number    *\n");
       printf("*between 1 and 13 that shows the face *\n");
       printf("*value of the card, where Jack is 11, *\n");
       printf("*Queen is 12, and King is 13 and then *\n");
       printf("*Indicate the suit of the card using  *\n");
       printf("*1 through 4, where a 1 is hearts, a  *\n");
       printf("*2 is spades, 3 is diamonds and  4 is *\n");
       printf("*clubs.  A win or a loss is declared. *\n");
       printf("***************************************\n\n");
    }
    
    
    
    
    /*********************************************
    ** Function: PrintCard
    ** Usage:    PrintCard(pip, suit);
    **
    ** Prints the name of the card.
    **
    ** Inputs: pip -  an integer between 1 and 13
    **                 indicating and Ace - King
    **	   suit - an integer between 1 and 4
    **	           indicating the card's suit
    ** Output: None 
    **
    ** Assumptions: Relies on #defines for suits and
    **              Ace, Jack, Queen & King.
    *********************************************/
    void PrintCard(int pip, int suit)
    {
       /*print the pip of the card*/
       switch (pip)
       {
          case ACE: 
    	 printf ("Ace ");
    	 break;
          case JACK:
    	 printf ("Jack ");
    	 break;
          case QUEEN:
    	 printf ("Queen ");
    	 break;
          case KING:
    	 printf ("King ");
    	 break;
          case 2:   
          case 3:   
          case 4:
          case 5:   
          case 6:   
          case 7:
          case 8:   
          case 9:   
          case 10:
    	 printf ("%2d ", pip);
    	 break;
          default: 
    	 printf("%d - Not Valid\n", pip);
    	 break;
       }
    
       /*print the suit of the card*/
       switch (suit)
       {
          case HEARTS: 
    	 printf ("of Hearts\n");
    	 break;
          case SPADES:
    	 printf ("of Spades\n");
    	 break;
          case DIAMONDS:
    	 printf ("of Diamonds\n");
    	 break;
          case CLUBS:
    	 printf ("of Clubs\n");
    	 break;
          default: 
    	 printf("%d - Not Valid\n", suit);
    	 break;
       }
    }
    
    
    /******************************************************************
    ** Function: PrintResults
    ** Usage:    PrintResults(pip, suit, playerPip, playerSuit);
    **
    ** Prints the two cards and the results of the game (win or lose)
    **
    ** Inputs: pip -  an integer between 1 and 13 indicating  
    **                 Ace - King that was drawn by the computer
    **	   suit - an integer between 1 and 4 indicating the
    **	           card's suit that was drawn by the computer
    **         playerPip - an integer between 1 and 13 
    **                      indicating Ace - King chosen by the user
    **	   playerSuit - an integer between 1 and 4
    **	                 indicating the card's suit chosen by the user
    ** Output: None 
    *****************************************************************/
    void PrintResults (int pip, int suit, int playerPip, int playerSuit)
    {
       /*print the cards*/
       printf("\n\nI picked the ");
       PrintCard(pip, suit);
       printf("and you picked the ");
       PrintCard(playerPip, playerSuit);
    
       /*compare the cards and declare winner pr loser*/
       if (pip == playerPip && suit == playerSuit)
       {
          printf("\nYou Win !!!\n\n");
       }
       else
       {
          printf("\nSorry, you lose\n\n");
       }
    }
    
    
    Explain


  • util.c
  • /*************************************************************************\
    * Filename:      util.c                                                   *
    * Author:        Sue Evans                                                *
    * Date Written:  9/18/08                                                  *
    * Section #:     02XX                                                     *
    * Email:         bogar@cs.umbc.edu                                        *
    *                                                                         *
    * This file contains functions that are modules to be used with many      *
    * C programs.  As new useful modules are written, we'll add them to this  *
    * file.  Modules should never be removed from this file even if they are  *
    * not being used with your current program.                               *
    *                                                                         *
    * Currently this file contains:                                           *
    *    GetValidInt() - a function that prompts the user to enter an         *
    *                    integer value in the range of two integers passed    *
    *                    in as arguments and will assure the value returned   *
    *                    is within that range, inclusive.                     *
    *    SetRandomSeed() - a function that seeds the random number generator  *
    *                      using a value from time() as the seed.             *
    *    GetRandomNumber() - this function returns a random number within     *
    *                        the range of the arguments passed in.            *
    \*************************************************************************/
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #include "util.h"
    
    /*********************************************
    ** Function: GetValidInt()
    ** GetValidInt() gets an integer from the user between
    ** specified minimum and maximum values, and will
    ** reprompt the user until it gets one that is valid.
    **                                                  
    ** Inputs: min and max, the minimum and maximum      
    **          (inclusive) values for the entered integer 
    ** Output: returns an integer between min and max,  
    **          inclusively                              
    *********************************************/    
    int GetValidInt(int min, int max)
    {
       /* is set greater than max so the loop will be entered*/
       int input = max + 1;
    
       /* Loop assures valid input */
       while( input < min || input > max )
       {
          printf("Please enter an integer between");
          printf(" %d and %d.\n", min, max);
          scanf("%d", &input);
       }
    
       return input;
    }
    
    
    
    /*********************************************
    ** Function: SetRandomSeed
    ** Usage:    SetRandomSeed();
    **
    ** The pseudo-random number generator is seeded
    ** with a call to time() assuring a different
    ** seed everytime the program runs.
    **
    ** Inputs: None
    ** Output: No value is returned
    *********************************************/
    void SetRandomSeed (void) 
    {
       int timeSeed ;
    
       /* Use the time function to set the seed. */
       timeSeed = (int) time(0) ;
       srand(timeSeed) ;
    }
    
    
    
    /*********************************************
    ** Function: GetRandomNumber
    ** Usage:    x = GetRandomNumber();
    **
    ** GetRandomNumber() returns a pseudo-random integer
    ** between the values passed into the function: low and 
    ** high, inclusive.
    **
    ** Inputs: low - an int which is the lowest value 
    **         in the range of acceptable random numbers
    **         high - an int which is the highest value
    **         in the range of acceptable random numbers
    ** Output: returns a random integer between low and 
    **         high, inclusive
    *********************************************/
    int GetRandomNumber (int low, int high) 
    {
       int num ;
    
       /* Call rand() to get a large random number. */
       num = rand() ;
    
       /* Scale the random number within range. */
       num = num % (high - low + 1) + low ;
    
       return num ;
    }
    
    
    Explain


  • cards.h
  • /*************************************************************************\
    * Filename:      cards.h                                                  *
    * Author:        Sue Evans                                                *
    * Date Written:  9/17/08                                                  *
    * Section #:     02XX                                                     *
    * Email:         bogar@cs.umbc.edu                                        *
    *                                                                         *
    * This file contains the function prototypes for the functions defined in *
    * the cards.c file.                                                       *
    \*************************************************************************/
    
    
    /*********************************************
    ** Function: PrintInstructions()               
    **                                            
    ** PrintInstructions() prints the instructions
    ** for the cards program                           
    **                                            
    ** Inputs: none                                
    ** Output: none (Side-effect: prints to screen)
    *********************************************/
    void PrintInstructions (void);
    
    
    /*********************************************
    ** Function: PrintCard
    ** Usage:    PrintCard(pip, suit);
    **
    ** Prints the name of the card.
    **
    ** Inputs: pip - an integer between 1 and 13
    **         indicating Ace - King
    **         suit - an integer between 1 and 4
    **         indicating the card's suit
    ** Output: None 
    **
    ** Assumptions: Relies on #defines for suits and
    **              Ace, Jack, Queen & King.
    *********************************************/
    void PrintCard(int pip, int suit);
    
    
    /******************************************************************
    ** Function: PrintResults
    ** Usage:    PrintResults(pip, suit, playerPip, playerSuit);
    **
    ** Prints the two cards and the results of the game (win or lose)
    **
    ** Inputs: pip - an integer between 1 and 13 indicating  
    **               Ace - King that was drawn by the computer
    **         suit - an integer between 1 and 4 indicating
    **               the card's suit that was drawn by the computer
    **         playerPip - an integer between 1 and 13 
    **                     indicating Ace - King chosen by the user
    **         playerSuit - an integer between 1 and 4
    **                      indicating the card's suit chosen by the user
    ** Output: None 
    *****************************************************************/
    void PrintResults (int pip, int suit, int playerPip, int playerSuit);
    
    Explain


  • util.h
  • /*************************************************************************\
    * Filename:      util.h                                                   *
    * Author:        Sue Evans                                                *
    * Date Written:  9/18/08                                                  *
    * Section #:     02XX                                                     *
    * Email:         bogar@cs.umbc.edu                                        *
    *                                                                         *
    * This file contains the prototypes for the functions defined in util.c   *
    \*************************************************************************/
    
    
    /*********************************************
    ** Function: GetValidInt()
    ** GetValidInt() gets an integer from the user between
    ** specified minimum and maximum values, and will
    ** reprompt the user until it gets one that is valid.
    **                                                  
    ** Inputs: min and max, the minimum and maximum      
    **          (inclusive) values for the entered integer 
    ** Output: returns an integer between min and max,  
    **          inclusively                              
    *********************************************/    
    int GetValidInt(int min, int max);
    
    
    /*********************************************
    ** Function: SetRandomSeed
    ** Usage:    SetRandomSeed();
    **
    ** The pseudo-random number generator is seeded
    ** with a call to time() assuring a different
    ** seed everytime the program runs.
    **
    ** Inputs: None
    ** Output: No value is returned
    *********************************************/
    void SetRandomSeed (void);
    
    
    /*********************************************
    ** Function: GetRandomNumber
    ** Usage:    x = GetRandomNumber();
    **
    ** GetRandomNumber() returns a pseudo-random integer
    ** between the values passed into the function: low and 
    ** high, inclusive.
    **
    ** Inputs: low - an int which is the lowest value 
    **         in the range of acceptable random numbers
    **         high - an int which is the highest value
    **         in the range of acceptable random numbers
    ** Output: returns a random integer between low and 
    **         high, inclusive
    *********************************************/
    int GetRandomNumber (int low, int high);
    
    
    
    

  • Compiling & linking
  • linuxserver1.cs.umbc.edu[101] ls
    bigcards.c   cards.c   cards.h   separate.c   util.c   util.h
    linuxserver1.cs.umbc.edu[102] gcc -c -Wall -ansi separate.c
    linuxserver1.cs.umbc.edu[103] gcc -c -Wall -ansi cards.c
    linuxserver1.cs.umbc.edu[104] gcc -c -Wall -ansi util.c
    linuxserver1.cs.umbc.edu[105] ls
    bigcards.c    cards.h    separate.c    util.c    util.o
    cards.c       cards.o    separate.o    util.h
    linuxserver1.cs.umbc.edu[106] gcc separate.o cards.o util.o
    linuxserver1.cs.umbc.edu[107] ls
    a.out         cards.c    cards.o       separate.o    util.h
    bigcards.c    cards.h    separate.c    util.c        util.o
    linuxserver1.cs.umbc.edu[108]
    
    Explain


  • Explanation of separate.c

  • Explanation of cards.c
  • This file contains functions that relate specifically to the current assignment, in this case, playing this card game. The functions in this file should not be functions that would be good modules to keep for the future. Useful modules would belong in the util.c file.


  • Explanation of util.c
  • This file contains functions that are designed to be good modules that can be used by this current assignment, may have been used previously, and will be used in the future for upcoming assignments. As you write modules that can be reused, they should be placed in this file. Functions should never be removed from the file, even if they aren't needed by this current assignment. It will do no harm to have additional function definitions in the file.


  • Explanation of cards.h

  • Explanation of util.h

  • Explanation of Compiling and linking

  • UMBC | CSEE | 201 | 201 F'08 | lectures | projects | help

    Friday, 19-Sep-2008 10:16:13 EDT