Home Segments Index Top Previous Next

416: Mainline

You can write a simple program to pick out just the right trades by inspecting the industry codes, doing nothing if the code is not the one you want. Suppose, for example, that you want to analyze only those trades with industry code 1 or 5, ignore those trades with industry code 0, 2, 3, or 4, and issue a warning for any other code. You need only to include the following in your program:

... 
/* Read trade information */ 
for (limit = 0; 3 == scanf ("%i%lf%i", &industry, &price, &number);) 
  if (industry == 1 || industry == 5) { 
    trade_pointers[limit] = 
      (struct trade*) malloc (sizeof (struct trade)); 
    trade_pointers[limit] -> price = price; 
    trade_pointers[limit] -> number = number; 
    ++limit; 
  } 
  else if (industry == 0 || industry == 2 
                           || industry == 3 
                             || industry == 4) 
    ; 
  else printf ("Industry code %i is unknown!\n", industry); 
... 

Note that ++limit has moved from the for statement to the if statement, because you add elements to the array only when you see a trade with an industry code of 1 or 5.