EGR 125 Introduction to Engineering Methods (C++) File: N125P3C Due date: Progra
ID: 3701165 • Letter: E
Question
EGR 125 Introduction to Engineering Methods (C++) File: N125P3C Due date: Programming Assignment #3: Projectile Tra jector A typical problem encountered in the study of dynamics is the trajectory problem. In the situation illustrated below, a projectile is fired from the edge of a cliff with an initial velocity, Vo, and a firing angle, A. The cliff has a height, h. It is desired to 1. determine the distance, xt, to the target 2. determine the highest elevation reached, ymax 3. determine the time to reach the target, t, 4. generate a table of (x.y) points to use in plotting the trajectory ymax Background If Vo-initial velocity and A-firing angle (in degrees), then V,,-initial horizontal velocity-V,cos(A) Vyoinitial vertical velocity Vosin(A) If g--9.81 m/s-acceleration due to gravity, then solving for t in the quadratic equation (the positive root) will give the time to reach the target, t(i.e., tt). Using this value of t, the distance to the target 1S and the maximum height reached isExplanation / Answer
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
using namespace std;
const float pi = 3.142;
const float g = -9.81;
struct input{
float A,h,v;
};
struct data{
float t,x,y;
};
float toradian(float deg);
void enterval(string prompt, float datapoint){
cout<<prompt;
cin>>datapoint;
}
void getInput(input &in){
do{
enterval("Enter angle(in degrees):",in.A);
}while(0>=in.A || in.A>=90);
in.A = toradian(in.A);
do{
enterval("Enter height(in meter):",in.h);
}while(in.h<=0);
do{
enterval("Enter initial velocity(in m/s):",in.v);
}while(in.v<=0);
}
float toradian(float deg){
return deg*pi/180;
}
void roots(float a,float b,float c,float &r1,float &r2){
float d = sqrt(b*b - 4*a*c)/(2*a);
r1 = r2 = -b/(2*a);
r1 += d;//add d to r1
r2 -= d;//add d to r2
}
data calTarget(input initval){
data tg;
float ti;
roots(0.5*g,initval.v*sin(initval.A),initval.h,tg.t,ti);
tg.x = cos(initval.A)*initval.v;
tg.y = 0;
}
data calPos(input in, float x){
data res;
res.x = x;
res.t = x/(in.v*cos(in.A));
res.y = in.h + in.v*cos(in.A)*res.t + 0.5*g*res.t*res.t;
return res;
}
data* calValues(input initval,int n){
data* values = new data[n];
data target = calTarget(initval);
int i;
for(i=0;i<n;i++){
values[i] = calPos(initval,target.x*i/(n-1));
}
return values;
}
void printRow(data val){
cout<<right<<setw(15)<<val.t<<'|';
cout<<right<<setw(15)<<val.x<<'|';
cout<<right<<setw(15)<<val.y<<endl;
}
void printValues(data *values,int n){
cout<<right<<setw(15)<<"Time(s)"<<'|';
cout<<right<<setw(15)<<"x(m)"<<'|';
cout<<right<<setw(15)<<"y(m)"<<endl;
for(int i=0;i<n;i++){
printRow(values[i]);
}
}
void saveRow(ofstream &out,data val){
out<<val.t<<',';
out<<val.x<<',';
out<<val.y<<' ';
}
void saveValues(data *values,int n){
string filename;
cout<<"Enter filename:";
cin>>filename;
ofstream out;
out.open(filename.c_str());
for(int i=0;i<n;i++){
saveRow(out,values[i]);
}
}
void choose(int &ch){
cout<<"1.Display values ";
cout<<"2.Save values ";
cout<<"Choice:";
cin>>ch;
}
int main(){
input initval;
getInput(initval);
int n=21;
data* values = calValues(initval,n);
int ch;
choose(ch);
switch (ch) {
case 1:
printValues(values,n);
break;
case 2:
saveValues(values,n);
break;
default:
printValues(values,n);
}
char rerun;
cout<<"Rerun the program(y/n)?";
cin>>rerun;
if(rerun == 'y'){
delete[] values;
main();
}
return 0;
}
//consider corrected y=h+0.5gt2+vt
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.