Alternatively, you can have the read expression place
numbers directly in the trade
structure variables, rather than
indirectly through
price
and number
variables:
#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[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) ; /* Add all products */ 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.