//Develop a program that gets from the user, the initial velocity of the missile and the desired altitude and displays the range of departure angles ϕ0 in degrees [degrees = radians(180/π)] to reach the desired altitude +/- 2%.

#include <stdio.h>
#include <math.h>
#define R 6371                      // defines the value for R globally
#define Escape_Velocity 11.2        // defines the value for the escape velocity globally
#define PI 3.141592654              // defines the value for PI globally

struct Global                       //Defines structure with structTypeName Global
{
    double min, max;                //Declares structure members min and max
};



struct Global Angle(double Altitude, double Initial_Velocity)       //Defines structure type (Function Angle)
{
    struct Global Departure_Angle, Alpha;                           //Declares variables to store calculation values in
    
    Alpha.min = (Altitude - (0.02 * Altitude))/R;           //An altitude range implies an Alpha range because Altitude = Alpha*R. Here a value is assigned to the minimum value of Alpha
    Alpha.max = (Altitude + (0.02 * Altitude))/R;           //Here a value is assigned to the maximum value of Alpha
    
    //minimum departure angle is calculated using maximum Atltitude hence max Alpha
    Departure_Angle.min = (Escape_Velocity/Initial_Velocity) * (Escape_Velocity/Initial_Velocity);
    Departure_Angle.min = (Alpha.max/(1+Alpha.max)) * Departure_Angle.min;
    Departure_Angle.min = sqrt(1-Departure_Angle.min);
    Departure_Angle.min = asin((1+Alpha.max)*Departure_Angle.min);
    Departure_Angle.min = (180/PI)*Departure_Angle.min;
    
    //maximum departure angle is calculated using minimum Atltitude hence min Alpha
    Departure_Angle.max = (Escape_Velocity/Initial_Velocity) * (Escape_Velocity/Initial_Velocity);
    Departure_Angle.max = (Alpha.min/(1+Alpha.min)) * Departure_Angle.max;
    Departure_Angle.max = sqrt(1-Departure_Angle.max);
    Departure_Angle.max = asin((1+Alpha.min)*Departure_Angle.max);
    Departure_Angle.max = (180/PI)*Departure_Angle.max;
    
    
    return Departure_Angle; //Returns Departure angles to main
}

int main()
{
    double Initial_Velocity, Altitude;  //Variables are declared in main()
    struct Global Departure_Angle;
    
    printf("Please enter the initial velocity of the missile in km/s: ");   //Initial Velocity is requested from User
    scanf("%lf", &Initial_Velocity);        //Value is assigned to Initial_Velocity
    
    printf("Please enter the desired altitude of the missile in Kilometers: ");//Altitude is requested from User
    scanf("%lf", &Altitude);                //Value is assigned to Altitude
    
    Departure_Angle = Angle(Altitude, Initial_Velocity);        //Calls on function (struct) Angle to calculate min and max Departure angles
    
    //Min and Max values are printed to user
    printf("For initial velocity %.02lf km/s and desired altitude %.02lf km +/- 2%% the departure angle must be between %.03lf and %.03lf", Initial_Velocity, Altitude, Departure_Angle.min, Departure_Angle.max);
    
    return 0;
}

