Home Segments Index Top Previous Next

293: Mainline

The following program creates an array of three trades, wires in data via assignment statements, and accesses the data in a printf statement:

#include   
/* Define the trade structure */ 
struct trade {double price; int number;}; 
/* Define trade_price */ 
double trade_price (struct trade t) { 
  return t.price * t.number; 
} 
/* Define trade array */ 
struct trade trades[3]; 
main ( ) { 
  /* Wire in sample data */ 
  trades[0].price = 10.2; trades[0].number = 600; 
  trades[1].price = 12.0; trades[1].number = 100; 
  trades[2].price = 13.2; trades[2].number = 200; 
  /* Display products */ 
  printf ("The trade prices are, %f, %f, and %f.\n", 
          trade_price (trades[0]), 
          trade_price (trades[1]), 
          trade_price (trades[2])); 
} 
--- Result --- 
The trade prices are, 6120.000000, 1200.000000, and 2640.000000.