program file compiles under Linux to create an object file

The program file p1.c below compiles under Linux to create an object file p1.o. It is to be linked with another file p2.o to create a running program, whole.


Solution:
/* p1.c
 */
#include <stdlib.h>
#include <stdio.h>

//  PURPOSE:  To tell the maximum length of a C-string.
#define MAX_LENGTH 64



//  PURPOSE:  To hold the number that the user enters.
int guessedNumber;

//  PURPOSE:  To hold the true random number.
extern int trueNumber;


//  PURPOSE:  To repeated use 'getInteger()' to get a value for 'guessedNumber'
// until that value is between 1 and 100.  No return value.
extern void enterGuess ();


//  PURPOSE:  To print 'msgCPtr', allow the user to enter an integer, and
// return that integer.
int getInteger (const char* msgCPtr
)
{
  char numberText[MAX_LENGTH];

  printf(msgCPtr);
  fgets(numberText,MAX_LENGTH,stdin);
  return(atoi(numberText));
}


//  PURPOSE:  To:
// * print "Too low!\n" and return 0 if guessedNumber < trueNumber,
// * print "Too high!\n" and return 0 if guessedNumber > trueNumber, or
// * print "Congratulations!\n" and return 1 if they are equal.
extern int wasGuessCorrect ();


//  PURPOSE:  To return a random number between 1 to 100.  No parameters.
int chooseRandomNumber ()
{
  return( (rand() % 100) + 1 );
}


//  PURPOSE:  To seed the random number generator with the number given in
// 'argv[1]' (if 'argc' is at least 2) and allow the user to guess a
// randomly-chosen number between 1 and 100 inclusive.  Returns
// 'EXIT_SUCCESS' to the OS after the number is guessed.
int main (int argc,
char* argv[]
)
{
  if  (argc > 1)
    srand(atoi(argv[1]));

  trueNumber = chooseRandomNumber();

  do
  {
    enterGuess();
  }
  while  ( wasGuessCorrect() == 0 );

  return(EXIT_SUCCESS);
}

Sample output:

[instructor@JoesLaptopFedora16 Assign1]$ gcc p1.c -c
[instructor@JoesLaptopFedora16 Assign1]$ gcc p2.c -c
[instructor@JoesLaptopFedora16 Assign1]$ gcc p1.o p2.o -o whole
[instructor@JoesLaptopFedora16 Assign1]$ ./whole 1
Please enter your guess 1-100: 50
Too low!
Please enter your guess 1-100: 75
Too low!
Please enter your guess 1-100: 87
Too high!
Please enter your guess 1-100: 81
Too low!
Please enter your guess 1-100: 84
Congratulations!
[instructor@JoesLaptopFedora16 Assign1]$ ./whole 2
Please enter your guess 1-100: 50
Too low!
Please enter your guess 1-100: 75
Too low!
Please enter your guess 1-100: 87
Too low!
Please enter your guess 1-100: 93
Too high!
Please enter your guess 1-100: 90
Too low!
Please enter your guess 1-100: 91
Congratulations!
[instructor@JoesLaptopFedora16 Assign1]$ ./whole 3
Please enter your guess 1-100: 50
Too high!
Please enter your guess 1-100: 25
Too low!
Please enter your guess 1-100: 37
Too low!
Please enter your guess 1-100: 43
Too low!
Please enter your guess 1-100: 47
Congratulations!
[instructor@JoesLaptopFedora16 Assign1]$

    (30 Points) Write an elegant program file p2.c that would compile to p2.o, and would link with p1.o to create a legal running program whole.

    Important: Include your p2.c with your submission!

    HINTS:
        What does p1.c need that it does not define?
        Create the individual object files and the complete executable with:

        linux> gcc -c p1.c             # creates p1.o
        linux> gcc -c p2.c             # creates p2.o
        linux> gcc p1.o p2.o -o whole  # creates whole
        linux> ./whole                 # runs whole


Learn More :