The following is a revised version of the
analyze_trades
program shown in Segment 295. This version
uses a trade
pointer parameter in the trade_price
function:
#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 trades[100]; main ( ) { /* Declare various variables */ int limit, counter; double sum = 0.0; /* Read numbers and stuff them into array */ for (limit = 0; 2 == scanf ("%lf%i", &trades[limit].price, &trades[limit].number); ++limit) ; /* Display value of shares traded */ for (counter = 0; counter < limit; ++counter) sum = sum + trade_price(&trades[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.