Script started on Mon 23 Apr 2001 05:01:05 PM EDT king(1)# cat bmi.c /* David R. Brooks MCS280, Winter 98, HW #1 Calculate a "body mass index" (BMI). */ #include #define POUNDS_TO_KG 0.4536 /* Note specification of units for user input */ #define INCHES_TO_M 0.0254 #define A_F 1.667 /* constants defined for modified BMI (BMI') */ #define A_L 0.833 int main(void) { double weight,height,body_fat,bmi,bmi_1; double fat_weight,lean_weight; /* NOTE: To print the character "%", use %%. This is required because a single % is interpreted by C as the start of a conversion specifier. */ printf("Give weight (pounds), height (inches), body fat (%%): "); scanf("%lf %lf %lf",&weight,&height,&body_fat); /* unit conversions -- note optional use of compound assignment operators */ weight*=POUNDS_TO_KG; height*=INCHES_TO_M; body_fat/=100.; /* Convert from percent to fraction. */ bmi=weight/height/height; fat_weight=body_fat*weight; lean_weight=(1.-body_fat)*weight; bmi_1=(A_F*fat_weight+A_L*lean_weight)/height/height;/* or /(height*height) */ printf("BMI = %.1lf, modified BMI = %.1lf\n",bmi,bmi_1); return 0; } king(2)# bmi Give weight (pounds), height (inches), body fat (%): 180 75 15 BMI = 22.5, modified BMI = 21.6 king(3)# cat dinosaur.c /* David Brooks Chapter 3, Exercise #4 -- calculate speed of a dinosaur */ #include #include double dino_speed(double foot_print,double stride); int main(void) { double foot_print,stride_length,speed; printf("Give foot print length and stride length in meters: "); scanf("%lf %lf",&foot_print,&stride_length); speed=dino_speed(foot_print,stride_length); printf("This dinosaur's speed is %.2lf m/s.\n",speed); return 0; } double dino_speed(double foot_print,double stride) { double s,v,leg_length; const double g=9.807; leg_length=foot_print*4.; s=stride/leg_length; v=(s-0.8)/1.33; return v*sqrt(g*leg_length); } king(4)# dinosaur Give foot print length and stride length in meters: .64 3.3 This dinosaur's speed is 1.84 m/s. king(5)# cat windchill.c /* David Brooks Chapter 3, Exercise #6 -- calculate windchill temperature */ #include #include #define FILENAME "windchill.dat" double windchill(double temp,double wind_speed); int main(void) { double temperature,wind_speed; FILE *in; int status; in=fopen(FILENAME,"r"); while (1) { status=fscanf(in,"%lf %lf",&temperature,&wind_speed); if (status == EOF) break; printf("For a temperature of %.1lf F and a wind speed of %.1lf mph,\n", temperature,wind_speed); printf("the wind chill temperature is %.0lf F.\n", windchill(temperature,wind_speed)); } fclose(in); return 0; } double windchill(double T,double V) { return (0.279*sqrt(V)+0.55-0.0203*V)*(T-91.4)+91.4; } king(6)# windchill For a temperature of 30.0 F and a wind speed of 30.0 mph, the wind chill temperature is 1 F. For a temperature of 20.0 F and a wind speed of 5.0 mph, the wind chill temperature is 15 F. For a temperature of -3.0 F and a wind speed of 15.0 mph, the wind chill temperature is -34 F. For a temperature of 79.0 F and a wind speed of 8.0 mph, the wind chill temperature is 77 F. For a temperature of 15.0 F and a wind speed of 20.0 mph, the wind chill temperature is -15 F. For a temperature of 25.0 F and a wind speed of 5.0 mph, the wind chill temperature is 20 F. For a temperature of 30.0 F and a wind speed of 15.0 mph, the wind chill temperature is 10 F. king(7)# exit king(8)# script done on Mon 23 Apr 2001 05:03:19 PM EDT