/* --------------------------------------------------------------------- ** Figure 17.9: Calculating wind speed. ** Figure 17.10: Printing the wind speed table. ** --------------------------------------------------------------------- */ #include "tools.h" #define N 5 typedef double velocity[3]; double sqr( double x ){ return x * x; } double speed( velocity v ){ return sqrt( sqr(v[0]) + sqr(v[1]) + sqr(v[2]) ); } void print_table( string names[], bool mask[], velocity w[] ); void main( void ) { int city; double windspeed; velocity wind [N]; bool mask[N] = { 0 }; /* Initialize all masks to false. */ string names[N+1] = { "Tweed", "Bradley", "Sikorsky", "Hamden MS", "Bridgeport", "--finish--" }; banner(); printf( "\n Wind Speed \n" ); for (;;) { city = menu_i( " Station reporting data:", N + 1, names ); if (city == N) break; /* User selected "quit" */ printf( " Enter wind components for %s: ", names[city] ); scanf( "%lg%lg%lg", &wind[city][0], &wind[city][1], &wind[city][2] ); mask[city] = true; windspeed = speed( wind[city] ); printf( "\t Wind speed is %g.\n", windspeed); } print_table( names, mask, wind ); bye(); } /* ---------------------------------------------------------------------- */ void print_table( string names[], bool mask[], velocity w[] ) { int k; puts( "\n Wind Speeds at Reporting Weather Stations" ); for (k = 0; k < N; ++k) { if (mask[k]) printf( " %-15s (%7.2f %7.2f %7.2f ) speed: %6.2f\n", names[k], w[k][0], w[k][1], w[k][2], speed(w[k]) ); } }