/* Homework Assignment #3 * * Duy Nguyen * Cedric Lam * mylogin@seas * EE5C Section 1A * * This program will determine the trajectory of a mortar shell. * */ #include #include const double G = 9.8; /* Gravity constant */ const NORMAL = 0; /* No Errors in Program */ const ERROR = -1; /* Encountered Error in Program */ /* Function Prototypes */ double get_angle(void); double get_speed(void); double calc_y_speed(double angle, double speed); double calc_x_speed(double angle, double speed); double calc_t_peak(double y_speed); double calc_y_peak(double y_speed, double t_peak); double calc_t_total(double t_peak); double calc_x_range(double x_speed, double t_total); void print_results(double angle, double speed, double t_total, double x_range, double y_peak); int main(void) { double angle, speed; double x_speed, y_speed, t_peak, t_total, y_peak, x_range; int status = NORMAL; /* Input Section */ speed = get_speed(); if (speed != ERROR) angle = get_angle(); /* Check to see whether we have valid input */ if ((speed == ERROR) || (angle == ERROR)) status = ERROR; /* If valid input, do calculations and print results */ if (status == NORMAL) { /* Calculations Section */ y_speed = calc_y_speed(angle, speed); x_speed = calc_x_speed(angle, speed); t_peak = calc_t_peak(y_speed); y_peak = calc_y_peak(y_speed, t_peak); t_total = calc_t_total(t_peak); x_range = calc_x_range(x_speed, t_total); /* Output Section */ print_results(angle, speed, t_total, x_range, y_peak); } return(status); } /* name: get_angle() * * purpose: get initial projectile angle from user * parameters: none * return value: 0 < tmp_angle < 90, or tmp_angle = ERROR */ double get_angle(void) { double tmp_angle; cout << "Enter mortar angle in degrees (relative to horizontal): "; cin >> tmp_angle; if (tmp_angle <= 0) { cout << "Don't shoot into the ground!\n"; return(ERROR); /* Tell main we had an error */ } else if (tmp_angle >= 90) { cout << "Let's try to shoot forward please!\n"; return(ERROR); /* Tell main we had an error */ } return(tmp_angle); } /* name: get_speed() * * purpose: get initial projectile speed from user * parameters: none * return value: 0 < tmp_speed, or tmp_speed = ERROR */ double get_speed(void) { double tmp_speed; cout << "Enter shell speed (in m/s): "; cin >> tmp_speed; if (tmp_speed <= 0) { cout << "Speed is positive!\n"; return(ERROR); /* Tell main we had an error */ } return(tmp_speed); } /* name: calc_y_speed() * * purpose: calculate initial projectile speed in y-direction * parameters: 0 < angle < 90, 0 < speed * return value: 0 < tmp_y_speed */ double calc_y_speed(double angle, double speed) { double tmp_y_speed; /* calculate y component of initial speed */ tmp_y_speed = speed * sin(angle*M_PI/180); return (tmp_y_speed); } /* name: calc_x_speed() * * purpose: calculate initial projectile speed in x_direction * parameters: 0 < angle < 90, 0 < speed * return value: 0 < tmp_x_speed */ double calc_x_speed(double angle, double speed) { double tmp_x_speed; /* calculate x component of initial speed */ tmp_x_speed = speed * cos(angle*M_PI/180); return (tmp_x_speed); } /* name: calc_t_peak() * * purpose: calculate time at peak of trajectory * parameters: 0 < y_speed * return value: 0 < tmp_t_peak */ double calc_t_peak(double y_speed) { double tmp_t_peak; /* calculate time at peak of trajectory */ tmp_t_peak = y_speed/G; return (tmp_t_peak); } /* name: calc_y_peak() * * purpose: calculate position at peak of trajectory * parameters: 0 < y_speed, 0 < t_peak * return value: 0 < tmp_y_peak */ double calc_y_peak(double y_speed, double t_peak) { double tmp_y_peak; /* calculate height at peak of trajectory */ tmp_y_peak = y_speed*t_peak - 0.5*G*t_peak*t_peak; return (tmp_y_peak); } /* name: calc_t_total() * * purpose: calculate total time of travel * parameters: 0 < t_peak * return value: tmp_t_total = 2*t_peak */ double calc_t_total(double t_peak) { double tmp_t_total; /* overall time of travel = 2 x time to peak */ tmp_t_total = t_peak * 2; return(tmp_t_total); } /* name: calc_x_range() * * purpose: calculate total horizontal distance travelled * parameters: 0 < t_peak * return value: 0 < tmp_x_range = t_peak * x_speed */ double calc_x_range(double x_speed, double t_total) { double tmp_x_range; /* calculate horizontal range */ tmp_x_range = t_total * x_speed; return (tmp_x_range); } /* name: print_results() * * purpose: output our findings to standard out * parameters: angle, speed, (input from users) t_total, x_range, y_peak (calculated) * return value: none */ void print_results(double angle, double speed, double t_total, double x_range, double y_peak) { cout << "For a mortar angle of "; cout.width(8); cout.setf(ios::fixed, ios::floatfield); cout.precision(2); cout << angle << " degrees\n"; cout << "at a speed of "; cout.width(8); cout.setf(ios::fixed, ios::floatfield); cout.precision(2); cout << speed << " m/s\n"; cout << "\nTotal travel time is "; cout.width(8); cout.setf(ios::fixed, ios::floatfield); cout.precision(2); cout << t_total << " seconds\n"; cout << "Horizontal range is "; cout.width(8); cout.setf(ios::fixed, ios::floatfield); cout.precision(2); cout << x_range << " meters\n"; cout << "Maximum vertical height is "; cout.width(8); cout.setf(ios::fixed, ios::floatfield); cout.precision(2); cout << y_peak << " meters\n"; }