Script started on Tue 16 Apr 2002 10:13:00 AM EDT king(1)# cat block.c /* Chapter 2, Exercise #2 */ #include #define G 9.8 /* gravitational acceleration, m/s^2 */ int main(void) { double ma,mb, /* masses, kg */ mu, /* coefficient of friction, dimensionless */ a; /* acceleration, m/s */ printf("Give two masses in kg: "); scanf("%lf %lf",&ma,&mb); printf("Give coefficient of friction for horizontal mass: "); scanf("%lf",&mu); printf("\nPART A:\n"); a = (ma-mu*mb)*G/(ma+mb); printf("The acceleration of the two masses is %.2lf m/s^2.\n",a); printf( "NOTE: Max coefficient of friction for which block will move is: %.2lf\n", ma/mb); printf("\nPART B:\n"); printf("For these same two masses hung over a frictionless pulley,\n"); printf("the acceleration of the \"a\" mass is %.2lf m/s^2.\n", (ma-mb)*G/(ma+mb)); printf("NOTE: A positive number indicates downward acceleration.\n"); return 0; } king(2)# cc block.c king(3)# a.out Give two masses in kg: 5 10 Give coefficient of friction for horizontal mass: .3 PART A: The acceleration of the two masses is 1.31 m/s^2. NOTE: Max coefficient of friction for which block will move is: 0.50 PART B: For these same two masses hung over a frictionless pulley, the acceleration of the "a" mass is -3.27 m/s^2. NOTE: A positive number indicates downward acceleration. king(4)# cat bmi.c /* Chapter 2, #18, body mass index */ #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(5)# cc bmi.c king(6)# a.out Give weight (pounds), height (inches), body fat (%): 200 75 15 BMI = 25.0, modified BMI = 24.0 king(7)# cat angles.c #include #define FILENAME "angles.dat" double convert(int deg,int min,int sec); int main(void) { int deg,min,sec,status; double degrees; FILE *in; in=fopen(FILENAME,"r"); while (1) { status=fscanf(in,"%d:%d:%d",°,&min,&sec); if (status == EOF) break; degrees=convert(deg,min,sec); printf("%02i:%02i:%02i = %lf degrees\n",deg,min,sec,degrees); } return 0; } double convert(int deg,int min,int sec) { return (double)deg+min/60.+sec/3600.; } king(8)# cat angles.dat 30:15:04 09:15:09 king(9)# cc angles.c king(10)# a.out 30:15:04 = 30.251111 degrees 09:15:09 = 9.252500 degrees king(11)# cat speed.c #include #define FILENAME "speed.dat" void main(void) { float time,miles_per_hour,feet_per_second,meters_per_second; const float miles_to_meters=5280.*12.*.0254; const float hours_to_seconds=3600.; const float miles_to_feet=5280.; int status; FILE *in; in=fopen(FILENAME,"r"); while (1) { status=fscanf(in,"%f",&time); if (status == EOF) break; printf("Time to run one mile (seconds): %.2f\n",time); miles_per_hour=hours_to_seconds/time; meters_per_second=miles_per_hour*miles_to_meters/hours_to_seconds; feet_per_second=miles_per_hour*miles_to_feet/hours_to_seconds; printf( "meters per second = %.2f\nfeet per second = %.2f\nmiles per hour = %.2f\n", meters_per_second,feet_per_second,miles_per_hour); } fclose(in); } king(12)# cat speed.dat 240 480 king(13)# cc speed.c king(14)# a.out Time to run one mile (seconds): 240.00 meters per second = 6.71 feet per second = 22.00 miles per hour = 15.00 Time to run one mile (seconds): 480.00 meters per second = 3.35 feet per second = 11.00 miles per hour = 7.50 king(15)# exit king(16)# script done on Tue 16 Apr 2002 10:14:16 AM EDT