Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I'm having an issue getting my decimals to 3 decimal places. Also my code isn't outputting like the picture below. I need it as close
I'm having an issue getting my decimals to 3 decimal places. Also my code isn't outputting like the picture below. I need it as close as possible to that. Please help! Thanks!
Code is below:
#include
#include
#include
#define PI 3.14159265
void updatePosition (double pos[],double vel[]);
void updateVelocity (double vel[],const double acc[],const double timeInc);
void outputPosition(double pos[]);
const double timeInc = 0.001;
const double initialVelocity = 110;
const double acc[3] = {0,0.25,-9.81};
int main()
{
double pos[3],vel[3];
double elevation = 62.900;
double azimuth = 2.9;
double val = PI / 180.0;
pos[0] =0;
pos[1] =0;
pos[2] =0;
double vel_g = initialVelocity * cos(elevation * val);
double vel_a = initialVelocity * sin(elevation * val);
vel[0] = vel_g * cos(azimuth * val);
vel[1] = vel_g * sin(azimuth * val);
vel[2] = vel_a;
double time =0;
do
{
updatePosition(pos,vel);
updateVelocity(vel,acc,timeInc);
printf("Time: %0.31f s", time);
outputPosition(pos);
time += timeInc;
}
while(pos[2] >0);
double diff_x=1000 - pos[0];
double diff = diff_x* diff_x;
double distance = sqrt(diff);
printf("Missed by %03.1f m ",distance);
printf("Author: Dalton Farrington ");
printf("Azimuth: %03.1f ",azimuth);
printf("Elevation angle: %03.1f ", elevation);
return 0;
}
void updatePosition(double pos[],double vel[])
{
pos[0] = vel[0]*timeInc + pos[0];
pos[1] = vel[1]*timeInc + pos[1];
pos[2] = vel[2]*timeInc + pos[2];
}
void updateVelocity(double vel[],const double acc[],const double timeInc)
{
vel[0] = vel[0] + acc[0] * timeInc;
vel[1] = vel[1] + acc[1] * timeInc;
vel[2] = vel[2] + acc[2] * timeInc;
}
void outputPosition(double pos[])
{
printf("X-Pos: %0.31f m", pos[0]);
printf("Y-Pos: %0.31f m", pos[1]);
printf("Height: %03.1f m ",pos[2]);
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started