Lab 8 Sample Answers

CSC121A - Berry College - Spring 2005

 

 

1. (Exercise 2.16) The value of RAND_MAX is 2147483647 - which is 2^31 - 1. Thus, it is the maximum possible positive value that can be stored in a signed int of 32 bits.

 

2. (Exercise 2.17) Here is a program that tests the variance between the pseudo-random numbers generated by the library rand() function. The difference oscillates near zero but gets as large as 50 sometimes (that is, sometimes up to 50 numbers in a row bigger or smaller than RANDMAX/2 are generated).

 

/********************************************/

#include <math.h>

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

 

int main(void) {

    int median = RAND_MAX/2;

    int i, plus_cnt, minus_cnt, max_diff;

    int seed = time(NULL);

    srand( seed );   /* seed the random number generator */

   

    for (i = plus_cnt = minus_cnt = max_diff = 0; i < 500; i++) {

        int diff;

        if (rand() > median) plus_cnt++;

        else minus_cnt++;

        diff = plus_cnt - minus_cnt;

        printf("%d\n", diff);

        if ( abs(diff) > max_diff ) max_diff = abs(diff);

    }

   

    printf("\nSeed: %d\n", seed);

    printf("Max difference: %d\n", max_diff);

   

    return 0;

}

/********************************************/

 

 

3. (Exercise 3.4) The code should execute and print out the following. Make sure you understand how the expressions were parsed and evaluated to obtain these results:

 

0 0

1 0

1 1

1 0

 

 

4. (Exercise 3.5) In both cases the assignment operator = is used instead of the equality operator ==. It will be easy to detect something is wrong in the loop because the loop will end up executing forever, since the loop test will always evaluate to 7 (non-zero = true). In the if statement however, the error is more subtle, and you might not even notice it unless you specifically were testing the case where k is not equal to 7. Like the loop, the body of the if statement will always be executed, but it will only happen once, so you won't notice it as much as an infinite loop.

 

5. (Exercise 3.6) The comparison between x+y and x-y will most probably return false because the range of digits between x and y is so large the computer cannot keep track of it in the fixed number of bits that it uses to store a double. Here is another sample program that illustrates this:

 

int main(void) {

    double x = 123456789101112.987654321;

    printf("%f\n", x);

    return 0;    

}

 

Try running it and see what happens.

 

6. (Exercise 3.20) The program should print the following. Again, make sure you understand how the expressions are parsed and evaluated.

 

0 0 0

0 0 1

 

7. (Exercise 4.16)

- If you change the format to %15s, the spacing gets messed up, because it prints more than 80 characters per line and so each line wraps around.

 

- If you move the srand() call to the inside of the for loop, it seeds (initializes) the random number generator each time around the loop. Since the loop iterates many times per second, this effectively resets the sequence of random numbers generated to the same starting point, so you end up only getting heads or tails. However, try tossing the coin a large number of times - such as 99999. This time, the loop will take longer than 1 second to finish, so you should see the heads/tails change a few times.

 

 

Homework Exercise

 

Below is my solution. (OK, I cheated and used arrays of strings, which we haven't talked about yet. Arrays and strings in C are a little different than Java so you have to be careful with them - just like everything else in C.)

 

/*

 *  datecompute.c

 *  Given a date, this determines the day of the week it fell on

 *  Works for dates back to 1753.

 *

 *  Nadeem Abdul Hamid  2/26/05.

 */

 

#include <stdio.h>

 

int main(void) {

 

   int res;   /* result of input operation */

   int day, month, year;  /* variables for input */

   int adjyear, A, B, C, D, W, X, Y, Z, R;  /* variables for Zeller's alg. */

  

   char* days[] = { "Sunday", "Monday", "Tuesday", "Wednesday",

                    "Thursday", "Friday", "Saturday" };

 

   do {

       printf("\nPlease enter date in the format mm/dd/yyyy: ");

       res = scanf("%d/%d/%d", &month, &day, &year);

       if ( res != 3 ) {

          printf("Incorrect date format. Program exiting.\n");

          return -1;

       }

   } while   ( day < 1 || day > 31 || month < 1 || month > 12 || year < 0 );

 

   /* zeller's algorithm */

   adjyear = year;   /* adjusted year */

   A = month - 2;    /* month of year, march has value 1... dec=10,

                    * jan/feb are 11/12 of preceding year */

   if ( A <= 0 ) { A += 12; adjyear--; }

   B = day;          /* day of the month */

   C = adjyear % 100;   /* year of the century */

   D = adjyear / 100;   /* the century */

  

   W = ( 13 * A - 1 ) / 5;

   X = C / 4;

   Y = D / 4;

   Z = W + X + Y + B + C - ( 2 * D );

   R = Z % 7;

   while ( R < 0 ) R += 7;   /* seems to need a while instead of */

                              /* just an if sometimes */

  

   printf( "\n%d/%d/%d is a %s.\n\n", month, day, year, days[R] );

  

   return 0;

}