CISP 400 C++ Programming
Please note, you are required to include the following when you use g++ to compile: -pedantic
For example:
g++ -pedantic filename.cpp
This will give a warning if you attempt to use any non-standard feature in g++. You must correct your code so that you do not receive any warnings of this type.
//Student header here
#ifndef VECTOR_H_DEF //DEF: These are my initials replace with yours
#define VECTOR_H_DEF //DEF: These are my initials replace with yours
#include <iostream>
#include <cmath> //if needed
using namespace std;
class Vector
{
double _x;
double _y;
double _z;
public:
Vector();
Vector(double X, double Y, double Z = 0.0);
void display() const; // <_x, _y, _z>
void add(const Vector&);
void sub(const Vector&);
void mult(const double&);
void div(const double&);
void normalize();
double length()const;
};
#endif
In Vector.cpp, implement these member functions.
In VectorMain.cpp, write a test main to test your code.
Submit these four files via the drop-box
Definitions:
<x1, y1, z1> + <x2, y2, z2> = <x1+x2, y1+y2, z1+z2>
<x1, y1, z1> - <x2, y2, z2> = <x1-x2, y1-y2, z1-z2>
<x, y, z> * d = <x*d, y*d, z*d>
<x, y, z> / d = <x/d, y/d, z/d>
|<x, y, z>| = Sqrt(x2 + y2 + z2) |v|: the length of v
normalize(v) = v/|v|
Can someone help me with this? I made the files 4 files
// Vector.h
#ifndef VECTOR_H_DEF //DEF: These are my initials replace with yours
#define VECTOR_H_DEF //DEF: These are my initials replace with yours
#include <iostream>
#include <cmath> //if needed
using namespace std;
class Vector
{
double _x;
double _y;
double _z;
public:
Vector();
Vector(double X, double Y, double Z = 0.0);
void display() const; // <_x, _y, _z>
void add(const Vector&);
void sub(const Vector&);
void mult(const double&);
void div(const double&);
void normalize();
double length()const;
};
#endif
//end of Vector.h
// Vector.cpp
#include "Vector.h"
Vector::Vector()
{}
Vector::Vector(double X, double Y, double Z ): _x(X), _y(Y), _z(Z)
{}
void Vector:: display() const // <_x, _y, _z>
{
cout<<"<"<<_x<<", "<<_y<<", "<<_z<<">";
}
void Vector:: add(const Vector &v)
{
_x += v._x;
_y += v._y;
_z += v._z;
}
void Vector:: sub(const Vector &v)
{
_x -= v._x;
_y -= v._y;
_z -= v._z;
}
void Vector::mult(const double &d)
{
_x *= d;
_y *= d;
_z *= d;
}
void Vector::div(const double &d)
{
if(d > 0) // avoid division by 0
{
_x /= d;
_y /= d;
_z /= d;
}
}
void Vector:: normalize()
{
div(length());
}
double Vector::length()const
{
return(sqrt(pow(_x,2)+pow(_y,2)+pow(_z,2)));
}
//end of Vector.cpp
// VectorMain.cpp : C++ driver program to test the Vector class
#include "Vector.h"
#include <iostream>
using namespace std;
int main()
{
Vector v1(2,3,4), v2(3,4,5), v3(5,1);
cout<<"V1 : ";
v1.display();
cout<<" V2 : ";
v2.display();
cout<<" V3 : ";
v3.display();
cout<<endl<<"Length of V1 : "<<v1.length()<<endl;
v1.normalize();
cout<<"After normalize , V1 : ";
v1.display();
cout<<endl;
v1.add(v2);
cout<<"After V1 + V2, V1 : ";
v1.display();
cout<<endl;
v2.sub(v3);
cout<<"After V2 - V3, V2: ";
v2.display();
cout<<endl;
v3.mult(5.2);
cout<<"After V3 * 5.2, V3 : ";
v3.display();
return 0;
}
//end of VectorMain.cpp
Output:
Get Answers For Free
Most questions answered within 1 hours.