#include <iostream> #include <iomanip> #define _USE_MATH_DEFINES #include <cmath
ID: 3743370 • Letter: #
Question
#include <iostream>
#include <iomanip>
#define _USE_MATH_DEFINES
#include <cmath>
using namespace std;
int main()
{
float v0, v0x, v0y, angle, g=9.81; //ahy que cambiar el hard code a cin
float thmax, tmax, t, xpos, ypos, vxt, vyt;
int maxIteractions, exit;
//while loop
do{
//interacion del usuario
cout << "Initial speed v0: ";
cin >> v0;
cout << endl;
cout << "Angle: ";
cin >> angle;
cout << endl;
//calcular velocidad incucal
v0x = v0 * cos (angle * M_PI/180);
v0y = v0 * sin (angle * M_PI/180);
//calcular velocidad final
cout << fixed << setprecision(2); //guarda 2 espacios decimales
cout << "v0x: " << v0x << endl;
cout << "v0y: " << v0y << endl;
thmax = v0y / g;
tmax = 2 * thmax;
cout << "tFinal: " << tmax << endl;
maxIteractions = ceil( tmax / 0.1 );
cout << "Number of measures : " << maxIteractions << endl;
cout << "t: secs. xpos. ypos. vx: vy. ";
for(int i = 0; i < maxIteractions; i++)
{
t = i * 0.1;
vxt = v0x;
xpos = vxt * t;
vyt = v0y - g * t;
ypos = v0y * t - 0.5 * g * pow(t, 2);
//imprime calculo
cout << "t" << i << ": " << t << " " << xpos << " " << ypos;
cout << " " << vxt << " " << vyt << " ";
}
xpos = vxt * tmax;
vyt = v0y - g * tmax;
ypos =v0y * tmax - 0.5 * g * pow(tmax, 2);
cout << "t" << maxIteractions << ": " << tmax << " " << xpos << " " << ypos;
cout << " " << vxt << " " << vyt << " ";
//preguntar si quiere salir del ptrograma
cout << "Si quiere terminar, presione 0: ";
cin >> exit;
cout << " ";
}
while (0 != exit);
cout << "Programa terminado ";
return 0;
}
Explanation / Answer
To understand the flow let us go through each line of the program step by step. Please read the comments for each line to get the meaning of each line.
#include <iostream> // line 1 pre-processor directive used for input output library, this will let // you use cout and cin in your program
#include <iomanip> // line 2 this library is for using manipulator function like setprecision used // later in program
#define _USE_MATH_DEFINES // line 3 this is a macro used along with cmath or math library to // define constants like PI(3.18) which are missing in standard // C++ library definition.
// NOTE : Please use this line 3 before including cmath in line 4
#include <cmath> // line 4 this library is used to define common mathematical functions
using namespace std;
int main() // line 5 main function will begin execution and has return type as an integer
{
float v0, v0x, v0y, angle, g=9.81; // line 6 declaration and initialization of
float thmax, tmax, t, xpos, ypos, vxt, vyt; // line 7 declaration of floating type variables
int maxIteractions, exit; // line 8 declaration of integer type variables
//while loop
do{ // line 9 do-while loop is being used, so this is the entry point // of loop, which will execute atleast once and it continues // to loop untill the while condition at the end is satisfied
//interacion del usuario
cout << "Initial speed v0: "; // line 10 used to print the statement inside quotes on console
cin >> v0; // line 11 Enter the value of v0 (initial speed) - a user input
cout << endl; // line 12 print a new line
cout << "Angle: "; // line 13 print "Angle" on console
cin >> angle; // line 14 read value of angle from user
cout << endl; // line 15 print a new line
//calcular velocidad incucal
v0x = v0 * cos (angle * M_PI/180); // line 16 calculate value of v0x as per formula, note : // here M_PI will be initialized as 3.1415 due to the use // of MATH_DEFINES in begining
v0y = v0 * sin (angle * M_PI/180); // line 17 similarly calculate value of v0y
//calcular velocidad final
cout << fixed << setprecision(2); // line 18 fixed is used to set fixed number of decimal // places for a floating value used with setprecision. Here // setprecision() function sets the number of values after // decimal fixed to only 2 values.
cout << "v0x: " << v0x << endl; // line 19 print value of v0x calculated on line 16
cout << "v0y: " << v0y << endl; // line 20 print value of v0y calculated on line 17
thmax = v0y / g; // line 21 calclate thmax by dividing v0y by g (initialized to // 9.81 on line 6
tmax = 2 * thmax; // line 22 set tmax as tmax multiplied by 2
cout << "tFinal: " << tmax << endl; // line 23 print value of tmax as "tFinal"
maxIteractions = ceil( tmax / 0.1 ); // line 24 set value of maxIteractions as ceil of tmax // divided by 0.1. Ceil is used to round of value computed // by tmax/0.1 to an integer value which is greater than or // equal to the computed value which be 7 here
cout << "Number of measures : " << maxIteractions << endl; // line 25 print value of // maxIteractions
cout << "t: secs. xpos. ypos. vx: vy. "; // line 26 prints value of header // seperated by tabs ( ) which will //be printed over the values under loop
for(int i = 0; i < maxIteractions; i++) // line 27 start of for loop initialized to 0 and will run // untill i is less than maxIteractions (7 here), so loop // will run for total 7 times here
{
t = i * 0.1; // line 28 t is computed as i multiplied by 0.1
vxt = v0x; // line 29 vxt is assigned with value of v0x
xpos = vxt * t; // line 30 xpos is calculated as vxt mulitplied by t
vyt = v0y - g * t; // line 31 vyt is calculated
ypos = v0y * t - 0.5 * g * pow(t, 2); // line 32 ypos is calculated. pow is used for power, i.e, // t calculated to power of 2, which is simply a square // of t here
//imprime calculo
cout << "t" << i << ": " << t << " " << xpos << " " << ypos; // line 33 as calculated, print // value of i, t, xpos and ypos tab wise so that they // align under header printed on line 26
cout << " " << vxt << " " << vyt << " "; // line 34 print values of vxt and vyt
} // line 35 end of for loop
xpos = vxt * tmax; // line 36 calculate value of xpos
vyt = v0y - g * tmax; // line 37 calculate value of vyt
ypos =v0y * tmax - 0.5 * g * pow(tmax, 2); // line 38 calculate value of ypos
cout << "t" << maxIteractions << ": " << tmax << " " << xpos << " " << ypos; // line 39 print // values
cout << " " << vxt << " " << vyt << " "; // line 40 print values
//preguntar si quiere salir del ptrograma
cout << "Si quiere terminar, presione 0: "; // line 41 print if you want to enter more value
cin >> exit; // line 42 read value of exit variable from user
cout << " "; // line 43 print newline
} // line 44 end of do-while loop
while (0 != exit); // line 45 check if user has entered 0 or some other // input, if user enters 0 if asked on line 41, then // program will terminate, otherwise for any other // value program will go back to line 10 and whole // process will be repeated as per user input
cout << "Programa terminado "; // line 46 prints program terminated only when user // has entered 0 on line 42
return 0; // line 47 return 0 (exit by returning 0 on termination // of program)
}
Sample Output on console:
Initial speed v0: 20
Angle: 9
v0x: 19.75
v0y: 3.13
tFinal: 0.64
Number of measures : 7
t: secs. xpos. ypos. vx: vy.
t0: 0.00 0.00 0.00 19.75 3.13
t1: 0.10 1.98 0.26 19.75 2.15
t2: 0.20 3.95 0.43 19.75 1.17
t3: 0.30 5.93 0.50 19.75 0.19
t4: 0.40 7.90 0.47 19.75 -0.80
t5: 0.50 9.88 0.34 19.75 -1.78
t6: 0.60 11.85 0.11 19.75 -2.76
t7: 0.64 12.60 -0.00 19.75 -3.13
Si quiere terminar, presione 0: 0
Programa terminado
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.