Guest User

Blah blah

a guest
Aug 5th, 2012
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.21 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <string.h>
  4. #define G 9.8
  5. #define PI M_PI
  6.  
  7. float degrees(float theta);
  8. float radians(float theta);
  9. float minTheta(float v, float d);
  10. float angle(float v, float h, float d);
  11.  
  12. int main(){
  13.  
  14.   float v, h, d, theta;
  15.   char choice[20];
  16.  
  17.   printf("Compute: distance or angle? ");
  18.   scanf("%s", choice);
  19.  
  20.   if((strstr(choice, "d") > 0) || (strstr(choice, "D") > 0)){ //broken enter choice
  21.     printf("Enter: speed height angle: ");
  22.     scanf("%f%f%f", &v, &h, &theta);
  23.  
  24.     theta = radians(theta); //runs radians function to convert input to radians.  
  25.  
  26.     d = ((v*cos(theta))*(v*sin(theta)+(sqrt(pow(v,2)*pow(sin(theta),2)-(float)2*G*h))))/G;
  27.     if(isnan(d)){ //MAKE FUNCTION???
  28.       printf("no solution");
  29.     }
  30.     else{
  31.       printf("distance is %.2f metres", d);
  32.     }
  33.   }
  34.   else if((strstr(choice, "a") > 0) || (strstr(choice, "A") > 0)){
  35.       printf("Enter: speed height distance: ");
  36.       scanf("%f%f%f", &v, &h, &d);
  37.  
  38.       theta = angle(v, h, d);      
  39.       if(theta == 0){
  40.     printf("no solution");
  41.       }
  42.       else{
  43.       printf("angle is %.2f degrees", theta);
  44.       }
  45.   }
  46.   return 0;
  47. }
  48.  
  49. //function to convert degrees to radians for use in trig functions.
  50. float radians(float theta){
  51.  
  52.   theta = theta * PI/180;
  53.  
  54.   return theta;
  55. }
  56.  
  57. //computes the lower limit of theta given by arctan(v^2/g*h) returns answer in radians.
  58. float minTheta(float v, float d){
  59.   float theta;
  60.   float tmp;
  61.  
  62.   tmp = (pow(v,2))/(G*d);
  63.  
  64.   theta = atan(tmp);  
  65.  
  66.   return theta;
  67. }
  68.  
  69. //function to convert radians to degrees for displayed output
  70. float degrees(float theta){
  71.   theta = theta * (180/(PI));
  72.   return theta;
  73. }
  74.  
  75. //calulates theta by trial and error
  76. float angle(float v, float h, float d){
  77.  
  78.   float dif;
  79.   float trial;
  80.   float theta;
  81.  
  82.   theta  = minTheta(v, d);
  83.  
  84.   while(theta <= ((PI)/2)){
  85.     trial = ((v*cos(theta))*(v*sin(theta)+(sqrt(pow(v,2)*pow(sin(theta),2)-(float)2*G*h))))/G;
  86.  
  87.     dif = fabsf((d - trial));
  88.  
  89.     if(dif < 0.0001){ //since answer only needs to be 2 decimal places, this tolerence is justified.
  90.       theta = degrees(theta);
  91.       return theta;
  92.     }
  93.       theta = theta + 0.000001;
  94.   }
  95.   return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment