Home Segments Index Top Previous Next

566: Mainline

The converted program, with 5-minute pauses, is as follows:

#include   
#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; 
  /* Declare trade_source, a pointer to a file-describing structure */ 
  FILE* trade_source; 
  while (1) { 
    /* Initialize sum */ 
    sum = 0.0; 
    /* Prepare a file-describing structure for reading */ 
    trade_source = fopen ("test.data", "r"); 
    /* Read numbers from the file and stuff them into array */ 
    for (limit = 0; 
         2 == fscanf (trade_source, "%lf%i", &price, &number); 
         ++limit) { 
      trade_pointers[limit]  
        = (struct trade*) malloc (sizeof (struct trade)); 
      trade_pointers[limit] -> price = price; 
      trade_pointers[limit] -> number = number; 
    } 
    /* Close source file /* 
    fclose (trade_source); 
    /* Find value of shares traded */ 
    for (counter = 0; counter < limit; ++counter) 
      sum = sum + trade_price(trade_pointers[counter]); 
    /* Display value of shares traded */ 
    printf ("The total value of the %i trades is %.2f.\n", 
                                    limit,       sum); 
  sleep (300); 
  } 
}