Home Segments Index Top Previous Next

476: Mainline

Now that you know how to use the bitwise-or operator and powers of 2 to set particular bits, you can modify the analyze_trades program such that it includes a status variable, status:

#include   
/* Define the trade apparatus */ 
struct trade {double price; int number;}; 
/* Define trade array */ 
struct trade *trade_pointers[100]; 
/* Define enumeration constants for bit positions */ 
enum {bad_size_bit = 1, bad_price_bit  = 2, 
      too_much_bit = 4, too_little_bit = 8}; 
main ( ) { 
  /* Declare various variables */ 
  int limit, counter, number; 
  double price, sum = 0.0; 
  /* Declare status byte */ 
  unsigned char status = 0; 
  /* Read trade information */ 
  for (limit = 0; 
       2 == scanf ("%lf%i", &price, &number) && limit < 100; 
       ++limit)  
    if (price > 0.0 && number > 0) { 
      trade_pointers[limit] 
        = (struct trade*) malloc (sizeof (struct trade)); 
      trade_pointers[limit] -> price = price; 
      trade_pointers[limit] -> number = number; 
    } 
    else { 
     if (price <= 0.0) status = status | bad_price_bit; 
     if (number <= 0) status = status | bad_size_bit; 
    } 
  if (limit < 10) status = status | too_little_bit; 
  else if (limit == 100) status = status | too_much_bit; 
  // Analyze status variable 
}