Question: In the program 3d-space.cpp, add functions that create and delete coordinate structs dynamically: // allocate memory and initialize Coord3D* createCoord3D(double x, double y, double z);

In the program 3d-space.cpp, add functions that create and delete coordinate structs dynamically:

// allocate memory and initialize Coord3D* createCoord3D(double x, double y, double z); // free memory void deleteCoord3D(Coord3D *p); 

A usage example:

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); } 

Expected output:

$ ./a.out Enter position: 10 20 30 Enter velocity: 5.5 -1.4 7.77 Coordinates after 10 seconds: 65 6 107.7

--------------------------------------------------------------------------------------------------------------

CODE:

#include

#include

#include

#include

#include

using namespace std;

struct Coord3D {

double x;

double y;

double z;

};

double length(Coord3D *p) {

double num1, num2;

num1 = (p->x * p->x) + (p->y * p->y) + (p->z * p->z); // point to the x,y,z using the p->

num2 = pow(num1, .5); // power of 1/2 since its a radical or some math thing like that idk

return num2;

}

Coord3D * fartherFromOrigin(Coord3D *p1, Coord3D *p2) {

double num1, num2, num3, num4, biggest ;

num1 = (p1->x * p1->x) + (p1->y * p1->y) + (p1->z * p1->z); // first vector

num2 = pow(num1, .5);

num3 = (p2->x * p2->x) + (p2->y * p2->y) + (p2->z * p2->z); // second vector

num4 = pow(num3, .5);

if (num2 > num4) // if the first vector is farther from zero then return the first one

return p1;

else

return p2;

}

void move(Coord3D *ppos, Coord3D *pvel, double dt) {

Coord3D &new_pos = *ppos; // pass the pointer to the new_pos

Coord3D &new_vel = *pvel; // pass the poiter to the new_vel

new_pos.x = new_pos.x + (new_vel.x * dt); // x prime = x + vel * dt

new_pos.y = new_pos.y + (new_vel.y * dt);

new_pos.z = new_pos.z + (new_vel.z * dt);

*ppos = new_pos;

}

Coord3D* createCoord3D(double x, double y, double z){ // allocate memory and initialize

}

void deleteCoord3D(Coord3D *p) { // free memory

}

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);

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!