Home Segments Index Top Previous Next

612: Mainline

Assembling all these functions, iterations, tests, and appropriate variable definitions together, you have the following program:

#include   
/* Define the trade structure */ 
struct trade {double price; int number;}; 
/* Define auxiliary functions */ 
double mean_price (struct trade **array, int length) { 
  int counter; double sum = 0.0;  
  for (counter = 0; counter < length; ++counter) 
    sum = sum + array[counter] -> price; 
  return sum / counter; 
} 
double mean_size (struct trade **array, int length) { 
  int counter; double sum = 0.0;  
  for (counter = 0; counter < length; ++counter) 
    sum = sum + array[counter] -> number; 
  return sum / counter; 
} 
/* Define trade array */ 
struct trade *trade_pointers[100]; 
main (int argument_count, char **argument_array) { 
  /* Declare various variables */ 
  int limit, counter, number; 
  double price; 
  /* Declare trade_source, a pointer to a file-describing structure */ 
  FILE* trade_source; 
  /* Declare variables for flag analysis */ 
  int argument_counter, string_counter; 
  /* Define flag switches */ 
  int mean_price_switch = 0; 
  int mean_size_switch = 0; 
  // Open file 
  /* Analyze flags */ 
  for (argument_counter = 1; 
       argument_counter < argument_count - 1; 
       ++argument_counter) 
    if (argument_array [argument_counter][0] == '-')  
      for (string_counter = 1; 
           argument_array[argument_counter][string_counter]; 
           ++string_counter) 
        switch (argument_array [argument_counter][string_counter]) { 
          case 'p': mean_price_switch = 1; break; 
          case 'n': mean_size_switch = 1; break; 
          default: 
            printf ("%c is not a recognized flag.\n",  
                 argument_array [argument_counter][string_counter]); 
            break; 
        } 
  // Process and close file 
  /* Perform actions as dictated by flag switches */ 
  if (mean_price_switch == 1) 
    printf ("The mean price per share of the trades is %.2f.\n", 
             mean_price (trade_pointers, limit)); 
  if (mean_size_switch == 1) 
    printf ("The mean number of shares traded is %f.\n", 
             mean_size (trade_pointers, limit)); 
}