Answered step by step
Verified Expert Solution
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 PARTICLEH #define PARTICLEH #include struct CoordD double x; double y; double z; ; double lengthconst CoordD p; return the coordinate that's farthest from the origin CoordD fartherFromOriginCoordD p CoordD p; advance the position of the coordinate traveling at a given speed for a given time void moveCoordDCoordD ppos, const CoordD pvel, double dt; struct Particle public: double x; double y; double z; double vx; double vy; double vz; static sizet getNumAllocations return numallocations; void operator newconst sizet t voidt; numallocations; return ::new Particle; void operator deletevoid const p numallocations; freep; private: static sizet numallocations; ; dynamically allocate memory for a particle and initialize it Particle createParticledouble x double y double z double vx double vy double vz; set its velocity to vx vy vz void setVelocityParticle p double vx double vy double vz; get its current position CoordD getPositionconst Particle p; update particle's position after elapsed time dt void moveParticleParticle p double dt; delete all memory allocated for the particle passed by pointer void deleteParticleconst Particle p; #endif PARTICLEH Please help with all tasks of lab. Task A Length and distance in D space The origin of the coordinate system is denoted by O and has coordinates A point Pxyz together with the origin, defines a D 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 D: class CoordD 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 lengthCoordD p; A usage example: int main CoordD pointP ; cout length&pointP endl; would print return EXITSUCCESS; 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 CoordD fartherFromOriginCoordD p CoordD p; 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 CoordD pointP ; CoordD pointQ ; cout "Address of P: &pointP endl; cout "Address of Q: &pointQ endl endl; CoordD ans fartherFromOrigin&pointP, &pointQ; cout "ans ans endl; So which point is farther? return EXITSUCCESS; 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 D 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 D coordinate system as its displacement per second in the coordinates x y and z: CoordD vel ; 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 moveCoordDCoordD ppos CoordD 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 CoordD 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 CoordD pos ; CoordD vel ; moveCoordD&pos, &vel, ; object pos gets changed cout pos.x pos.y pos.z endl; prints: return EXITSUCCESS; 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
The header file:
#ifndef PARTICLEH
#define PARTICLEH
#include
struct CoordD
double x;
double y;
double z;
;
double lengthconst CoordD p;
return the coordinate that's farthest from the origin
CoordD fartherFromOriginCoordD p CoordD p;
advance the position of the coordinate traveling at a given speed for a
given time
void moveCoordDCoordD ppos, const CoordD pvel, double dt;
struct Particle
public:
double x;
double y;
double z;
double vx;
double vy;
double vz;
static sizet getNumAllocations return numallocations;
void operator newconst sizet t
voidt;
numallocations;
return ::new Particle;
void operator deletevoid const p
numallocations;
freep;
private:
static sizet numallocations;
;
dynamically allocate memory for a particle and initialize it
Particle createParticledouble x double y double z
double vx double vy double vz;
set its velocity to vx vy vz
void setVelocityParticle p double vx double vy double vz;
get its current position
CoordD getPositionconst Particle p;
update particle's position after elapsed time dt
void moveParticleParticle p double dt;
delete all memory allocated for the particle passed by pointer
void deleteParticleconst Particle p;
#endif PARTICLEH
Please help with all tasks of lab.
Task A Length and distance in D space
The origin of the coordinate system is denoted by O and has coordinates
A point Pxyz together with the origin, defines a D 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 D:
class CoordD
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 lengthCoordD p; A usage example:
int main
CoordD pointP ;
cout length&pointP endl; would print
return EXITSUCCESS;
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
CoordD fartherFromOriginCoordD p CoordD p; 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
CoordD pointP ;
CoordD pointQ ;
cout "Address of P: &pointP endl;
cout "Address of Q: &pointQ endl endl;
CoordD ans fartherFromOrigin&pointP, &pointQ;
cout "ans ans endl; So which point is farther?
return EXITSUCCESS;
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 D 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 D coordinate system as its displacement per second in the coordinates x y and z:
CoordD vel ; 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 moveCoordDCoordD ppos CoordD 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 CoordD 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
CoordD pos ;
CoordD vel ;
moveCoordD&pos, &vel, ; object pos gets changed
cout pos.x pos.y pos.z endl;
prints:
return EXITSUCCESS;
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
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started