Home Segments Index Top Previous Next

674: Mainline

Of course, you would never remember whether 0 means fast or accurate. Accordingly, you should test the value of mode against mnemonic enumeration constants. You probably should also replace the if statement with a switch statement, anticipating that you might eventually have more than two possible values for the mode global variable:

int mode; 
enum {fast, accurate}; 
... 
double mean_price (struct trade **array, int length) { 
  int counter, selector, increment; double sum = 0.0;  
  switch (mode) { 
    case fast:     increment = 2; break; 
    case accurate: increment = 1; break; 
  } 
  for (counter = 0, selector = 0; 
       selector < length; 
       ++counter, selector = selector + increment) 
    sum = sum + array[selector] -> price; 
  return sum / counter; 
}