#include <math.h> /* required for sin() and cos() */

#include <stdlib.h> /* required for random() */

//#define pi 3.14159265358979323846264 /*macro text replacement*/

#define pi 3.14

#define G 9.81

#define DELTA 0.01

int global_distance;

float global_velocity;

int global_angle;

/* these global variables are used to pass data from the main function to the simulate

function */

double last_iteration_x0;

double last_iteration_y0;

double last_iteration_t;

/* these global variables are used to store x0, y0 and t values, before the next iteration

made y0 negative */

double simulate (double x0, double y0, double vx, double vy);

int main()

{

int i;

int distance;

float velocity;

int angle;

srand( (unsigned)time( NULL ) ); /* ensures that random number is not fixed */

printf("Calculating random distance between 60 meters and 120 meters\n");

distance=random(121); /* generates random number between 0 and 120 */

while (distance<60 | distance>120) /* ensures that random number is between 60 and 120 */

{

distance=random(121);

}

printf("Distance = %d meters\n", distance);

double v2=sqrt(distance*G);

printf("\n");

printf("Calculating random velocity between %2.1f m/sec and %2.1f m/sec\n",v2, v2+20 );

velocity=random(v2+21); /* generates random number between 0 and sqrt(distance)*G + 20 */

while (velocity<v2 | velocity>v2+20)

/* ensures that random number is between sqrt(distance)*G and sqrt(distance)*G + 20 */

{

velocity=random(121);

}

printf("Velocity = %2.1f m/sec\n", velocity);

printf("\n");

printf("This program has calculated a distance of %d meters and a velocity of %2.1f m/sec\n", distance, velocity);

global_distance = distance;

global_velocity = velocity;

printf("\n");

printf("Guess an angle between 0 AND 90 degrees ");

scanf(" %d", &angle); /* Ampersand required */

while (angle<0 | angle>90) /* ensures that random number is between 60 and 120 */

{

printf("Wrong angle\n");

printf("\n");

printf("Guess an angle between 0 AND 90 degrees ");

scanf(" %d", &angle); /* Ampersand required */

}

global_angle = angle;

printf("You have entered an angle of %d degrees \n", angle);

double x_velocity = cos(angle * pi/180 )*velocity;

double x_position = (x_velocity*0) - distance;

double y_velocity = sin(angle * pi/180 )*velocity;

double y_position = y_velocity*0 - 0.5*G*pow(0,2);

double l = simulate (x_position, y_position, x_velocity, y_velocity);

char guess[2];

//guess[2] is char array holds the y or n value

printf("\n");

if (l>0)

printf("The ball flew long by %2.0f meters\n", l);

if (l<0)

printf("The ball flew short by %2.0f meters\n", l);

printf("Would you like to guess again? (y/n) \n");

printf("Enter y for YES or n for NO ");

scanf(" %s", guess);

while (guess[0]!=121 & guess[0]!=110)

// 121 is the unicode representation of y

// 110 is the unicode representation of n

{

printf("\n");

printf("Please enter y or n ");

scanf(" %s", guess);

}

while (guess[0]==121) // while guess = YES

{

if (l>10 | l<-10)

{

printf("\n");

printf("Enter another angle between 0 AND 90 degrees ");

scanf(" %d", &angle); /* Ampersand required */

while (angle<0 | angle>90) /* ensures that random number is between 60 and 120 */

{

printf("Wrong angle\n");

printf("\n");

printf("Enter an angle between 0 AND 90 degrees ");

scanf(" %d", &angle); /* Ampersand required */

}

global_angle = angle;

printf("You have entered an angle of %d degrees \n", angle);

x_velocity = cos(angle * pi/180 )*velocity;

x_position = - (distance);

y_velocity = sin(angle * pi/180 )*velocity;

y_position = 0;

l = simulate (x_position, y_position, x_velocity, y_velocity);

if (l<=10 & l>=-10)

break;

printf("\n");

if (l>0)

printf("The ball flew long by %2.0f meters\n", l);

if (l<0)

printf("The ball flew short by %2.0f meters\n", l);

}

printf("\n");

printf("Would you like to guess again? (y/n) \n");

printf("Enter y for YES or n for NO ");

scanf(" %s", guess);

}

if (l<=10 & l>=-10)

{

printf("\n");

printf("Congratulations: good angle!\n");

if (l>0)

printf("The ball flew long by %2.0f meters\n", l);

if (l<0)

printf("The ball flew short by %2.0f meters\n", l);

printf("This is within ten meters of the plate\n");

double C_angle1 = (180.0/pi) * 0.5 * asin(global_distance*G/pow

double C_angle2 = 90 - C_angle1;

printf("The calculated angles are %2.2f degrees and %2.2f degrees \n", C_angle1, C_angle2);

printf("\n");

//break;

}

if (guess[0]==110) // while guess = NO

{

printf("\n");

printf("Goodbye!\n");

double C_angle1 = (180.0/pi) * 0.5 * asin(global_distance*G/pow

double C_angle2 = 90 - C_angle1;

printf("The calculated angles are %2.2f degrees and %2.2f degrees \n", C_angle1, C_angle2);

}

return 0; /* Always best to do this */ }

double simulate (double x0, double y0, double vx, double vy)

{

int a; // loop counter1

int b=0; // loop counter2

double t=0; // time starts at 0

for (a=0; a<700; a++)

{

if (y0 < 0) // loop until y0 becomes negative

{

printf("Time = %2.2f seconds ", last_iteration_t);

printf("x_position = %2.2f meters ", last_iteration_x0);

printf("y_position = %2.2f meters ", last_iteration_y0);

printf("\n");

break; // stop the iteration

}

last_iteration_t=t;

last_iteration_x0=x0;

last_iteration_y0=y0;

if (a==b)

{

b = b+100;

printf("Time = %2.2f seconds ", t);

printf("x_position = %2.2f meters ", x0);

printf("y_position = %2.2f meters ", y0);

printf("\n");

}

vx = cos(global_angle * pi/180 )*global_velocity;

x0 = (vx*t) - global_distance;

vy = sin(global_angle * pi/180 )*global_velocity;

y0 = (vy*t) - (0.5*G*pow(t,2));

t = t+DELTA;

}

return last_iteration_x0;

}