L-13 MCS 260 Wed 19 Sep 2001

Below are the complete versions of the programs we discussed in class.
/* L-13 MCS 260 Wed 19 Sep 2001 : test on assert 

Remove the comments in the next preprocessor directive to ignore
the assert.  Note that the order of directives is important... */

/* #define NDEBUG */

#include<stdio.h>
#include<assert.h>

int main(void)
{
   int n;

   printf("Give a positive number : ");
   scanf("%d", &n);

   assert(n > 0);

   printf("Asserted that number is positive.\n");
   printf("You may try again with zero or negative number.\n");

   if (n <= 0)
      printf("The NDEBUG was defined, so the assertion was ignored.\n");

   return 0;
}
The next program is again a game. Please note that the design of the program (i.e: the choice of the functions) is as important of the actual C code below.
/* L-13 MCS 260 Wed 19 Sep 2001 : master mind 

The purpose of the game is that the user guesses a secret number,
generated at random by the computer.  After each guess the computer
evaluates the guess, printing the number of matching and digits in
the guess that do not occur in the secret word.
(For sake of "easy" implementation we have changed the rules a bit.)

In the implementation below, the user is first asked to provide the
number of digits in the secret word.

By the way, if you are interested in optimal strategies to play
this game (or better, if you want to instruct a computer how to 
play the game), then see

     Donald E. Knuth: "The Computer as Master Mind",
     J. Recreational Mathematics, 9 (1976-77), 1-6.
or
     Kenji Koyama, Tony W. Lai: "An optimal Mastermind Strategy",
     J. Recreational Mathematics, 1994.

*/

int generate_number();
/* generates a random number of the size provided by the user */

int matching_digits ( int a, int b );
/* returns the number of positions where the digits in a and b match */

int occurs ( int n, int digit );
/* returns 0 if the digit does not occur in n, 1 otherwise */

int not_occurring_digits ( int a, int b );
/* returns the number of digits of b that do not occur in the number a */

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<assert.h>

int main(void)
{
   int secret = generate_secret();
   int guess,matched,not_occur;

   do
   {
      printf("Make a guess : "); scanf("%d", &guess);
      matched = matching_digits(secret,guess);
      not_occur = not_occurring_digits(secret,guess);
      printf("  Number of matching digits  : %d\n", matched);
      printf("  Number of not occurring digits : %d\n", not_occur);

   } while (guess != secret);

   return 0;
}

int generate_secret()
{
   int i,size,pow10,secret; 

   srand(time(NULL));

   printf("How many digits in the secret number ? ");
   scanf("%d", &size);

   assert(size < 10);

   pow10 = 1;
   for (i = 0; i < size; i++) pow10*=10;

   secret = rand() % pow10;

   return secret;
}

int matching_digits ( int a, int b )
{
   int cnt = 0;
   int wa,wb;     /* working variables */

/* observe the use of the comma operator in the for : */

   for(wa=a,wb=b; (wa > 0) && (wb > 0); wa/=10,wb/=10)
      cnt += ((wa % 10) == (wb % 10));

   return cnt;
}

int occurs ( int n, int digit )
{
   int wn;

   for(wn = n; wn > 0; wn/= 10)
     if (digit == wn % 10) return 1;

   return 0;
}

int not_occurring_digits ( int a, int b )
{
   int cnt = 0;
   int wb;

   for (wb = b; wb > 0; wb/=10)
      if (occurs(a,wb %10) == 0) cnt++;

   return cnt;
}