Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The header file: #ifndef PARTICLE _ H #define PARTICLE _ H #include struct Coord 3 D { double x; double y; double z; } ;

The header file:
#ifndef PARTICLE_H
#define PARTICLE_H
#include
struct Coord3D {
double x;
double y;
double z;
};
double length(const Coord3D* p);
// return the coordinate that's farthest from the origin
Coord3D* fartherFromOrigin(Coord3D* p1, Coord3D* p2);
// advance the position of the coordinate traveling at a given speed for a
// given time
void moveCoord3D(Coord3D* ppos, const Coord3D* pvel, double dt);
struct Particle {
public:
double x;
double y;
double z;
double vx;
double vy;
double vz;
static size_t getNumAllocations(){ return _num_allocations; }
void* operator new(const size_t t){
(void)t;
_num_allocations++;
return ::new Particle();
}
void operator delete(void *const p){
_num_allocations--;
free(p);
}
private:
static size_t _num_allocations;
};
// dynamically allocate memory for a particle and initialize it
Particle* createParticle(double x, double y, double z,
double vx, double vy, double vz);
// set its velocity to (vx, vy, vz)
void setVelocity(Particle* p, double vx, double vy, double vz);
// get its current position
Coord3D getPosition(const Particle* p);
// update particle's position after elapsed time dt
void moveParticle(Particle* p, double dt);
// delete all memory allocated for the particle passed by pointer
void deleteParticle(const Particle* p);
#endif // PARTICLE_H
Please help with all tasks of lab.
Task A. Length and distance in 3D space
The origin of the coordinate system is denoted by O and has coordinates (0,0,0).
A point P=(x,y,z), together with the origin, defines a 3D vector OP. The distance from O to P, or in other words, the length of the vector OP can be computed using the euclidean distance formula, see below:
We are provided with a class type that represents coordinates in 3D:
class Coord3D {
public:
double x;
double y;
double z;
};
Write a function a function called length() that receives the coordinates of a point P passed as a pointer, and computes the distance from the origin to the point P:
double length(Coord3D *p); A usage example:
int main(){
Coord3D pointP ={10,20,30};
cout << length(&pointP)<< endl; // would print 37.4166
return EXIT_SUCCESS;
}
Notice that we pass the memory address &pointP, where the object of this class is located. The function should dereference this address to get the corresponding fields x, y, and z for computing the length.
Task B. Farther from the origin? Add a function
Coord3D * fartherFromOrigin(Coord3D *p1, Coord3D *p2); Which receives the coordinates of two points (passed as pointers), and returns the pointer of the point that is farther away from the origin.
A usage example:
int main(){
Coord3D pointP ={10,20,30};
Coord3D pointQ ={-20,21,-22};
cout << "Address of P: "<< &pointP << endl;
cout << "Address of Q: "<< &pointQ << endl << endl;
Coord3D * ans = fartherFromOrigin(&pointP, &pointQ);
cout << "ans ="<< ans << endl; // So which point is farther?
return EXIT_SUCCESS;
}
When testing your code, look at the reported address of the answer ans and determine whether it reports P or Q.
Task C. Velocity and moving objects
Lets consider an object moving in 3D space. We already know how to describe its position. (We will be assuming metric system, thus distances will be implicitly measured in meters and time in seconds.)
The objects velocity can be represented in the same 3D coordinate system as its displacement per second in the coordinates x, y, and z:
Coord3D vel ={5,-3,1}; // x, y, z components of the velocity
When moving at constant velocity vel , the objects new position after the elapsed time dt can be computed as
x'= x + vel.x * dt;
y'= y + vel.x * dt;
z'= z + vel.x * dt;
Lets implement it. In the same program, write a function
void moveCoord3D(Coord3D *ppos, Coord3D *pvel, double dt);
which gets the position and the velocity of an object and has to compute objects new coordinates after the time interval dt. The function does not return any values, instead, it should update the objects position ppos with its new position coordinates.
Because we pass the coordinates Coord3D * ppos as a pointer, all changes to the fields of the class pointed by ppos, will affect the original object you pass into the function, not its local copy. Example:
int main(){
Coord3D pos ={0,0,100.0};
Coord3D vel ={1,-5,0.2};
moveCoord3D(&pos, &vel, 2.0); // object pos gets changed
cout << pos.x <<""<< pos.y <<""<< pos.z << endl;
// prints: 2-10100.4
return EXIT_SUCCESS;
}
Task D. Creating and deleting objects dynamically
Add functions that create, delete, and coordinate objects dynamically:
// allocate memory and initialize
Task E. Working with Particles
Write functions that:
store position and velocity of the particle, and
provide the following programming interface:
// dynamically allocate memory for

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions