Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help with all tasks of lab. Here is my header.h #ifndef PARTICLE _ H #define PARTICLE _ H #include struct Coord 3 D {

Please help with all tasks of lab.
Here is my header.h
#ifndef PARTICLE_H
#define PARTICLE_H
#include
struct Coord3D {
double x;
double y;
double z;
};
// get the distance from the given coordinate to the origin
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
My Current Code. I am keep running into errors and I am not sure what's wrong?
#include
#include
struct Coord3D {
double x;
double y;
double z;
};
double length(const Coord3D* p){
double dist = std::sqrt(p->x * p->x + p->y * p->y + p->z * p->z);
return dist;
}
Coord3D* fartherFromOrigin(Coord3D* p1, Coord3D* p2){
double dist1= length(p1);
double dist2= length(p2);
if (dist1> dist2){
return p1;
} else {
return p2;
}
}
void moveCoord3D(Coord3D* ppos, const Coord3D* pvel, double dt){
ppos->x += pvel->x * dt;
ppos->y += pvel->y * dt;
ppos->z += pvel->z * dt;
}
size_t Particle::_num_allocations =0;
Particle* createParticle(double x, double y, double z,
double vx, double vy, double vz){
Particle* particle = new Particle();
particle->x = x;
particle->y = y;
particle->z = z;
particle->vx = vx;
particle->vy = vy;
particle->vz = vz;
return particle;
}
void setVelocity(Particle* p, double vx, double vy, double vz){
p->vx = vx;
p->vy = vy;
p->vz = vz;
}
Coord3D getPosition(const Particle* p){
Coord3D pos ={ p->x, p->y, p->z };
return pos;
}
void moveParticle(Particle* p, double dt){
moveCoord3D(&(getPosition(p)), &(Coord3D{ p->vx, p->vy, p->vz }), dt);
}
void deleteParticle(const Particle* p){
delete p;
}
int main(){
Coord3D pointP ={10,20,30};
std::cout << "Length of pointP: "<< length(&pointP)<< std::endl;
Coord3D pointQ ={-20,21,-22};
Coord3D* farthest = fartherFromOrigin(&pointP, &pointQ);
std::cout << "Farthest from origin: ("<< farthest->x <<","<< farthest->y <<","<< farthest->z <<")"<< std::endl;
Coord3D pos ={0,0,100.0};
Coord3D vel ={1,-5,0.2};
moveCoord3D(&pos, &vel, 2.0);
std::cout << "New position: ("<< pos.x <<","<< pos.y <<","<< pos.z <<")"<< std::endl;
Particle* particle = createParticle(0,0,100.0,1,-5,0.2);
std::cout << "Current position: ("<< getPosition(particle).x <<","<< getPosition(particle).y <<","<< getPosition(particle).z <<")"<< std::endl;
moveParticle(particle,2.0);
std::cout << "New position: ("<< getPosition(particle).x <<","<< getPosition(particle).y <<","<< getPosition(particle).z <<")"<< std::endl;
deleteParticle(particle);
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Database Marketing The Ultimate Marketing Tool

Authors: Edward L. Nash

1st Edition

0070460639, 978-0070460638

Students also viewed these Databases questions