Write C++ program with the instructions below (working on the 2 functions). (Ple
ID: 3717744 • Letter: W
Question
Write C++ program with the instructions below (working on the 2 functions). (Please answer soon as it is due in a few hours, thank you so much.)
This is the code written out so far:
#include
#include
using namespace std;
struct Coord3D{
double x;
double y;
double z;
};
double length(Coord3D *p){
double sqrs=(pow(p->x,2)+pow(p->y,2)+pow(p->z,2));
return sqrt(sqrs);
}
void move(Coord3D *ppos, Coord3D *pvel, double dt){
ppos->x = (pvel->x * dt + ppos->x);
ppos->y = (pvel->y * dt + ppos->y);
ppos->z = (pvel->z * dt + ppos->z);
}
Coord3D* createCoord3D(double x, double y, double z){
//allocating memory and initializing using "new"
}
void deleteCoord3D(Coord3D *p){
//deleting the memory hold and freeing space using "delete"
}
int main() {
double x, y, z;
cout << "Enter position: ";
cin >> x >> y >> z;
Coord3D *ppos = createCoord3D(x,y,z);
cout << "Enter velocity: ";
cin >> x >> y >> z;
Coord3D *pvel = createCoord3D(x,y,z);
move(ppos, pvel, 10.0);
cout << "Coordinates after 10 seconds: "<< (*ppos).x << " " << (*ppos).y << " " << (*ppos).z << endl;
deleteCoord3D(ppos); // release memory
deleteCoord3D(pvel);
}
In the program dynamically: 3d-space.cpp , add functions that create and delete coordinate structs 7allocate memory and initialize Coord3D* createCoord3D (double x, double y, double z); /7 free memory void deleteCoord3D (Coord3D *p); A usage example int main() l double x, y, z; coutExplanation / Answer
#include <iostream>
#include <cmath>
using namespace std;
struct Coord3D{
double x;
double y;
double z;
};
double length(Coord3D *p){
double sqrs=(pow(p->x,2)+pow(p->y,2)+pow(p->z,2));
return sqrt(sqrs);
}
void move(Coord3D *ppos, Coord3D *pvel, double dt){
ppos->x = (pvel->x * dt + ppos->x);
ppos->y = (pvel->y * dt + ppos->y);
ppos->z = (pvel->z * dt + ppos->z);
}
Coord3D* createCoord3D(double x, double y, double z){
//allocating memory and initializing using "new"
Coord3D* coordPoints = new Coord3D;
coordPoints->x = x;
coordPoints->y = y;
coordPoints->z = z;
return coordPoints;
}
void deleteCoord3D(Coord3D *p){
//deleting the memory hold and freeing space using "delete"
elete p;
}
int main() {
double x, y, z;
cout << "Enter position: ";
cin >> x >> y >> z;
Coord3D *ppos = createCoord3D(x,y,z);
cout << "Enter velocity: ";
cin >> x >> y >> z;
Coord3D *pvel = createCoord3D(x,y,z);
move(ppos, pvel, 10.0);
cout << "Coordinates after 10 seconds: "<< (*ppos).x << " " << (*ppos).y << " " << (*ppos).z << endl;
deleteCoord3D(ppos); // release memory
deleteCoord3D(pvel);
}
Output:
Enter position: 10 20 30
Enter velocity: 5.5 -1.4 7.77
Coordinates after 10 seconds: 65 6 107.7
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.