Home Segments Index Top Previous Next

377: Mainline

Combining the pieces, you have the following program:

#include   
/* Define the trade structure */ 
struct trade {double price; int number;}; 
/* Define value-computing function */ 
double trade_price (struct trade *tptr) { 
  return tptr -> price * tptr -> number; 
} 
/* Define trade array */ 
struct trade *trade_pointers[100]; 
main ( ) { 
  /* Declare various variables */ 
  int limit, counter, number; 
  double price, sum = 0.0; 
  /* Read numbers and stuff them into array */ 
  for (limit = 0; 
       2 == scanf ("%lf%i", &price, &number); 
       ++limit) { 
    trade_pointers[limit]  
      = (struct trade*) malloc (sizeof (struct trade)); 
    trade_pointers[limit] -> price = price; 
    trade_pointers[limit] -> number = number; 
  } 
  /* Display value of shares traded */ 
  for (counter = 0; counter < limit; ++counter) 
    sum = sum + trade_price(trade_pointers[counter]); 
  printf ("The total value of the %i trades is %f.\n", 
                                  limit,       sum); 
} 
--- Data ---
10.2    600 
12.0    100 
13.2    200 
--- Result --- 
The total value of the 3 trades is 9960.000000.