CPS 202
Mount
Quiz 4

Answers

1. What is wrong with this function? It compiles fine, but that's about all that's right. Point out two problems. (5 points)

// Bump up a counter by one each time we call this function
int counter(void)
{
  static int count = 0;
  count++;
  return(count);
}

The function has several problems. The main problem is that count is automatic, not static, meaning its value is not retained from call to call. In addition, because count is never initialized, its value is unknown at all times. Finally, the function returns an int, as declared in the function declaration, but never actually returned a value. By adding the code in red above, this function is usable.


2. These fragments of code from a larger program have a big problem. Whenever it runs, it prints that the total is 0. Why? How would you fix it so it will print "366"? (15 points)

int total = 0;
int data[5] = { 88, 92, 86, 56, 44 };

void accumulate_totals(void)
{
  int n;
  for (n=0; n<5; n++)
    put_in_total(data[n]);
  printf("The total is: %d\n", total);
  return;
}

void put_in_total(int d)
{
  // static int total = 0;
  printf("Adding %d to total...\n",d);
  total += d;
  return;
}

The easiest way to make this code do what it should, to total the values in the data[] array, is to add the comment characters (//), as shown in red above. This forces the total with global scope to be used to total the values, and not a local variable. To further improve this fragment, the put_in_total() function should be reevaluated - it may not be needed.


3. Write a program to flip a pair of coins some number of times, as defined by the user. Use random-number generation to pick the value for each coin in each flip (see below). When the coins have been flipped as many times as the user requested, tell the user how many times each combination (heads|heads, tails|tails, heads|tails, tails|heads) was flipped. In this program, you must use at least one user-defined function and at least one array. (30 points)

Code to generate a random number between 1 and 10. To use rand() and srand() you must include stdlib.h. To use time(), you must include time.h.

int number;
srand(time(NULL));      // Seed the random number generator - only do this once!!
number = (rand() % 10) + 1;

Here is an example:

Enter how many times to flip two coins: 3
tails tails
tails heads
tails heads

Totals:
 tails, tails = 1
 tails, heads = 2
 heads, tails = 0
 heads, heads = 0
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

void initialize(void);
int  flip_coins(void);
void print_totals(int []);

main()
{
  int flip_combos[4] = {0};
  int flips;
  int flip;
  int i;

  printf("Enter how many times to flips two coins: ");
  scanf("%d",&flips);

  initialize();

  for (i=0; i<flips; i++) {
    flip = flip_coins();
    switch(flip) {
      case 0: printf("tails tails\n");
              break;
      case 1: printf("tails heads\n");
              break;
      case 2: printf("heads tails\n");
              break;
      case 3: printf("heads heads\n");
              break;
    }
    flip_combos[flip]++;
  }

  print_totals(flip_combos);

  return(0);
}

void initialize(void)
{
  srand((unsigned int)time(NULL));
  return;
}

int flip_coins(void)
{
  static int coin1, coin2;

  coin1 = rand() % 2;    // Generate number, 0 or 1
  coin2 = rand() % 2;    // Generate number, 0 or 1
  if (coin1 == 0 && coin2 == 0) return(0);  // tails tails
  if (coin1 == 0 && coin2 == 1) return(1);  // tails heads
  if (coin1 == 1 && coin2 == 0) return(2);  // heads tails
  return(3);                                // heads heads
}

void print_totals(int flips[])
{
  int i;
  printf("\nTotals:\n");
  printf("tails, tails = %d\n",flips[0]);
  printf("tails, heads = %d\n",flips[1]);
  printf("heads, tails = %d\n",flips[2]);
  printf("heads, heads = %d\n",flips[3]);
  return;
}

3a. Extra credit. Is there any difference between flipping two coins to get one of the four possibilities, and rolling a four-sided die to get one of the four possibilities? How would you prove your answer? (10 points)

There appears to be no difference between flipping two coins and flipping one (theoretical) four-sided coin. The result in either case is roughly the same, about 25% of each possibility. This is not always the case ... you cannot get the same result from flipping two dice as from flipping one twelve-side die. To prove it, we can change the flip_coins function to roll a four-sided die, and over several runs, see if the two versions produce the same results.

int flip_coins(void)
{
  static int coin;

  coin = rand() % 4;    // Generate number, 0 - 3
  return(coin);
}

4. Given the following declarations, create a memory map. Each cell in the map should be a single byte. Assume 16-bit ints. Fill each cell in the map with a hex value for the cell (hint: use the Windows Calculator to convert a decimal number to a hex number [See View|Scientific]). (25 points)

short short_val = 0;
long long_val = 3735928559;
int array1[5] = { 1024, 2048, 4096, 8192, 16384 };
char array2[5] = { 'C', 'A', 'P', 'E', 'R' };


5. True/False (5 points)


6. What does "void" mean? Where would you use it? Give an example. (10 points)

The void keyword is primarily used as a function return value, meaning that the function returns no meaningful value, or in a function's parameter list, to indicate that no arguments are passed when the function is called.

void fn(void); // returns noting, takes no parameters


7. Explain what you would use a function stub for. (10 points)

A function stub is used to mark a place where a function will go, before it has been written. The stub is just the name of the function with an empty block, which gives the linker something to grab onto.


Back to Main CPS 202 Page

Last Modified: 8 Apr 2002