Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- #include <string.h>
- #define G 9.8
- #define PI M_PI
- float degrees(float theta);
- float radians(float theta);
- float minTheta(float v, float d);
- float angle(float v, float h, float d);
- int main(){
- float v, h, d, theta;
- char choice[20];
- printf("Compute: distance or angle? ");
- scanf("%s", choice);
- if((strstr(choice, "d") > 0) || (strstr(choice, "D") > 0)){ //broken enter choice
- printf("Enter: speed height angle: ");
- scanf("%f%f%f", &v, &h, &theta);
- theta = radians(theta); //runs radians function to convert input to radians.
- d = ((v*cos(theta))*(v*sin(theta)+(sqrt(pow(v,2)*pow(sin(theta),2)-(float)2*G*h))))/G;
- if(isnan(d)){ //MAKE FUNCTION???
- printf("no solution");
- }
- else{
- printf("distance is %.2f metres", d);
- }
- }
- else if((strstr(choice, "a") > 0) || (strstr(choice, "A") > 0)){
- printf("Enter: speed height distance: ");
- scanf("%f%f%f", &v, &h, &d);
- theta = angle(v, h, d);
- if(theta == 0){
- printf("no solution");
- }
- else{
- printf("angle is %.2f degrees", theta);
- }
- }
- return 0;
- }
- //function to convert degrees to radians for use in trig functions.
- float radians(float theta){
- theta = theta * PI/180;
- return theta;
- }
- //computes the lower limit of theta given by arctan(v^2/g*h) returns answer in radians.
- float minTheta(float v, float d){
- float theta;
- float tmp;
- tmp = (pow(v,2))/(G*d);
- theta = atan(tmp);
- return theta;
- }
- //function to convert radians to degrees for displayed output
- float degrees(float theta){
- theta = theta * (180/(PI));
- return theta;
- }
- //calulates theta by trial and error
- float angle(float v, float h, float d){
- float dif;
- float trial;
- float theta;
- theta = minTheta(v, d);
- while(theta <= ((PI)/2)){
- trial = ((v*cos(theta))*(v*sin(theta)+(sqrt(pow(v,2)*pow(sin(theta),2)-(float)2*G*h))))/G;
- dif = fabsf((d - trial));
- if(dif < 0.0001){ //since answer only needs to be 2 decimal places, this tolerence is justified.
- theta = degrees(theta);
- return theta;
- }
- theta = theta + 0.000001;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment