Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

need help on c++ program please here\'s code: // TestThreeDVec.cpp // // COSC103

ID: 3827530 • Letter: N

Question

need help on c++ program please

here's code:

// TestThreeDVec.cpp
//
// COSC1030, Sp 2017
// Program 13
//
// Test program to demonstrate and validate desired behavior
// of the new C++ class ThreeDVec .
//
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include "ThreeDVec.h"

int main()
{
ThreeDVec A;
cout << "A is " << A << endl << endl;
ThreeDVec X(1,0,0), Y(0,1,0), Z(0,0,1);
cout << "X+Y+Z is " << X+Y+Z << endl << endl;

ThreeDVec B(3,4,5);
cout << "B is " << B << endl;
cout << "||B|| is " << B.mag() << endl;
cout << "B dot Y is " << B*Y << endl << endl;;

ThreeDVec C(X+Y+Z);
cout << "C is " << C << endl;
cout << "B cross C is " << (B^C) << endl;
cout << "B dot (B cross C) is " << B*(B^C) << endl << endl;

ThreeDVec D;
cout << "Enter your vector coefficients as x y z: ";
cin >> D;
cout << "D is " << D << " with magnitude " << D.mag() << endl;
ThreeDVec E=D*(1/D.mag()); // This is a scalar multiplying a vector
cout << "Normalized D is |" << E << "| = " << E.mag() << endl << endl;

return 0;
}

Your boss needs a C++ data class which represents three-dimensional vectors (commonly used in physical sciences and engineering to assist in the analysis of phenomena including forces and electromagnetic fields). 1. The class you design must duplicate precisely the public interface shown in the at- tached main test program, TestThreeDVec.cpp. The particulars of the implemen- tation are vours to define. Note that this time. you have to create both the .h and the .cpp files. 2. This defines a vector in a 3D space NOT a STL vector-type object. These 3D vectors only ever store three values, an r, y, and 2. 3. Examples of the common notation and operations associated with such vectors are as follows: (a) Assume two vectors M Gr1, 1, 21) and N (z2, y2, 22) The scalar elements zi, yi, and zi should be assumed to take on real (floating point) values. (b) The sum is defined as: M +N r2, 1 1/2, 21 22), a vector quantity. T1 (c) The magnitude is defined as llMI Varf yf 2?, a scalar quantity. (This is can also be written M (zi yi 21)1/2.) (d) The dot product is defined as: M *N (z yit/2 2122), a scalar quantity. a e) The cross product is defined as: M N (y122 212/2, 21T2 T122, T12/2 13), a vector quantity. (Yes, the cross product usually uses x but we are using an operator we can overload easily.

Explanation / Answer

//ThreeDVec.h file

#ifndef THREEDVEC_H_INCLUDED
#define THREEDVEC_H_INCLUDED
#include <iostream>

using namespace std;

class ThreeDVec
{
float x,y,z; //the three coordinates of the vector
public:
ThreeDVec(); //default constructor
ThreeDVec(float l,float m,float n); //constructor
//overload << & >> operators, these must be global and as they need to access private data members they mus also be friends
friend ostream & operator << (ostream &out,const ThreeDVec &t);
friend istream & operator >> (istream &in, ThreeDVec &t);
ThreeDVec operator + (ThreeDVec t); //add two vectors
ThreeDVec operator ^ (ThreeDVec t); //cross product of two vectors
double operator * (ThreeDVec t); //multiply two vectors and return a scalar
double mag(); //magnitude of the vector
ThreeDVec operator *(double k); //multiply the vector by the given scalar and return a vector
void operator = (ThreeDVec t); //overload the assignment operator
};


#endif // THREEDVEC_H_INCLUDED

//threeDVec.cpp file

#include <iostream>
#include <cmath>
#include "ThreeDVec.h"

ThreeDVec::ThreeDVec() //default constructor
{
x=y=z=0;
}
ThreeDVec::ThreeDVec(float l, float m,float n) //constructor
{
x=l;
y=m;
z=n;
}
double ThreeDVec::mag() //magnitude of the vector
{
double res=sqrt(x*x + y*y + z*z);
return res;
}
ThreeDVec ThreeDVec::operator *(double k) //multiply the vector by the given scalar and return a vector
{
ThreeDVec temp(x*k,y*k,z*k);
return temp;
}
double ThreeDVec::operator * (ThreeDVec t) //multiply two vectors and return a scalar
{
double res;
res=x*t.x + y*t.y + z*t.z;
return res;
}
ThreeDVec ThreeDVec::operator + (ThreeDVec t) //add two vectors
{
ThreeDVec res;
res.x=x + t.x;
res.y=y + t.y;
res.z=z + t.z;
return res;
}
ThreeDVec ThreeDVec::operator ^ (ThreeDVec t) //cross product of two vectors
{
ThreeDVec res;
res.x=y*t.z - z*t.y;
res.y=z*t.x - x*t.z;
res.z=x*t.y - y*t.x;
return res;
}
void ThreeDVec::operator=(ThreeDVec t) //overload assignment operator
{
x=t.x;
y=t.y;
z=t.z;
}
//overload << & >> operators, these must be global and as they need to access private data members they mus also be friends
istream & operator >> (istream &in, ThreeDVec &t)
{
in>>t.x;
in>>t.y;
in>>t.z;
return in;
}
ostream & operator << (ostream &out,const ThreeDVec &t)
{
out<<t.x;
out<<t.y;
out<<t.z;
return out;
}

//TestThreeDVec.cpp

// TestThreeDVec.cpp
//
// COSC1030, Sp 2017
// Program 13
//
// Test program to demonstrate and validate desired behavior
// of the new C++ class ThreeDVec .
//
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include "ThreeDVec.h"
#include "ThreeDVec.cpp"

int main()
{
ThreeDVec A;
cout << "A is " << A << endl << endl;
ThreeDVec X(1,0,0), Y(0,1,0), Z(0,0,1);
cout << "X+Y+Z is " << X+Y+Z << endl << endl;
ThreeDVec B(3,4,5);
cout << "B is " << B << endl;
cout << "||B|| is " << B.mag() << endl;
cout << "B dot Y is " << B*Y << endl << endl;;

ThreeDVec C(X+Y+Z);
cout << "C is " << C << endl;
cout << "B cross C is " << (B^C) << endl;
cout << "B dot (B cross C) is " << B*(B^C) << endl << endl;
ThreeDVec D;
cout << "Enter your vector coefficients as x y z: ";
cin >> D;
cout << "D is " << D << " with magnitude " << D.mag() << endl;
ThreeDVec E=D*(1/D.mag()); // This is a scalar multiplying a vector
cout << "Normalized D is |" << E << "| = " << E.mag() << endl << endl;
return 0;
}

Hope this helps!