Exercise 4 - CBD page 258 - Kurt Nørmark

Solution index                   Textual C program


/* Programmed by Lone Leth Thomsen, modified by Kurt Nørmark
   March 2003  */

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

typedef enum fish {bass, salmon, shrimp, trout} fish;   
typedef enum meat {veal, beef, pork} meat;              
typedef enum fruit {apple, peach, pear} fruit;          

void prnt_fish(fish f){
 switch (f){
  case bass: printf("%10s", "Bass"); break;
  case salmon: printf("%10s", "Salmon"); break;
  case shrimp: printf("%10s", "Shrimp"); break;
  case trout: printf("%10s", "Trout"); break;
 }
}

void prnt_meat(meat m){
 switch (m){
  case veal: printf("%10s", "Veal"); break;
  case beef: printf("%10s", "Beef"); break;
  case pork: printf("%10s", "Pork"); break;
 }
}

void prnt_fruit(fruit f){
 switch (f){
  case apple: printf("%10s", "Apple"); break;
  case peach: printf("%10s", "Peach"); break;
  case pear: printf("%10s", "Pear"); break;
 }
}

void meal(void){
 int fi = rand() % 4;
 int me = rand() % 3;  
 int fr = rand() % 3;   

 prnt_fish(fi); printf("  ");
 prnt_meat(me); printf("  ");
 prnt_fruit(fr); printf("\n");
}

int main(void){
 int i;

 srand(time(NULL));
 for (i=0; i < 20; i++)
  meal();

 return 0;
}

typedef enum fish {bass, salmon, shrimp, trout} fish;   
typedef enum meat {veal, beef, pork} meat;              
typedef enum fruit {apple, peach, pear} fruit;          
Three enumeration types are defined, and they are immediately put into typedefs which makes the enumeration types available using the simple names fish, meat, and fruit. This provides for a nice and clean application of the new types.
 

void prnt_fish(fish f){
 switch (f){
  case bass: printf("%10s", "Bass"); break;
  case salmon: printf("%10s", "Salmon"); break;
  case shrimp: printf("%10s", "Shrimp"); break;
  case trout: printf("%10s", "Trout"); break;
 }
}
Given a fish as input the name of the fish is printed. We use a switch control structure to implement a mapping from the internal fish number to the (printing of) the name of the fish. As an alternative it would be able to return the fish name as a string. Both solutions are tedious and error prone. It would be nice if we had access to the names of enumeration constants at run time - but this is not the case. The alternative solution (the function that returns a string) would be better and more versatile in many situations. We avoid it here because we have not yet studied strings in C. The other functions prnt_meat and prnt_fruit are similar.
 

void meal(void){
 int fi = rand() % 4;
 int me = rand() % 3;  
 int fr = rand() % 3;   

 prnt_fish(fi); printf("  ");
 prnt_meat(me); printf("  ");
 prnt_fruit(fr); printf("\n");
}
The function meal (actually a procedure without parameters) is assumed to print a random meal. We draw three random numbers. Random numbers returned by rand happen to be integers between 0 and the symbolic constant RAND_MAX. On my Linux machine RAND_MAX is 32767. The three random numbers are normalized to the proper interval by a modulus calculation (such as rand() % 3), and passed as input to prnt_fish, prnt_meat, and prnt_fruit, hereby causing a three dish meal to be printed. Notice, that it might have been appropriate to use symbolic constants (introduced with #define) for the number of items in each food group. It is almost always bad style to have constants, such as 3 and 4, appear directly in the program.
 

int main(void){
 int i;

 srand(time(NULL));
 for (i=0; i < 20; i++)
  meal();

 return 0;
}
In main we first set the seed of the random generator. For a given seed, the random numbers will be the same each time the program is executed. We set the seed to the current time, measured as the number of second passed since new years eve 1970. At the time of this dissection, the number is 1048101278. If you call the 'function' it will of course be slightly larger. With this non-constant seed we will experience different results each time we run the program. Here is an example:
    Shrimp        Pork        Pear
    Salmon        Beef        Pear
    Salmon        Pork       Apple
     Trout        Pork        Pear
    Salmon        Beef       Peach
      Bass        Beef       Apple
    Salmon        Beef       Peach
      Bass        Beef       Apple
    Salmon        Beef       Apple
    Salmon        Beef       Peach
    Salmon        Beef       Apple
     Trout        Veal       Apple
     Trout        Veal       Apple
      Bass        Pork        Pear
     Trout        Beef       Peach
     Trout        Veal       Apple
    Shrimp        Pork        Pear
    Shrimp        Pork        Pear
    Salmon        Beef       Apple
      Bass        Pork        Pear
In a for loop we call meal 20 times, hereby causing 20 (maybe different) meals to be printed. There are 4 * 3 * 3 different meal combinations, of course.
 


Generated: Wednesday, March 29, 2006, 12:33:30
This program dissection page is generated from an XML-in-LAML source file