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

In C ++ write a code following these guidelines: Part 2 In vector algebra we def

ID: 3824260 • Letter: I

Question

In C ++ write a code following these guidelines:

Part 2

In vector algebra we define a three dimensional vector v to be a on ordered triple v (x, y, z) where x, y and z are real numbers. We also define vector addition to be component wise this means that v (s, t, u) and w (x, y, z) then v w (s+x, t+y, u+z). 1. Create a new vector class in C++ that has a constructor that initializes its instances to (0, 0, 0). 2. Add set Components function that will mutate (modify) the vector instance and set its components to the three parameters x, y, z passes to the function respectively. 3. Add an add function add(v) that adds v to the current vector. 4. Add a display function that displays the vector as triple (x, y, z) 5. Write a main function that creates two instances and correctly add them display all three vectors. My Complex class should serve as a good guideline.

Explanation / Answer

#include <iostream>
#include <cmath>
using namespace std;

class Vector
{
private:
float x;
float y;
float z;
  
public:
Vector()//default constructor
{
x = 0;
y = 0;
z = 0;
}
Vector(float x,float y,float z)//argument constructor
{
this->x = x;
this->y = y;
this->z = z;
  
}
void setComponents(float x,float y,float z)//set method
{
this->x = x;
this->y = y;
this->z = z;
}
void add(Vector v)// function to add two vectors
{
this->x = v.x;
this->y = v.y;
this->z = v.z;
}
void display()
{
cout<<"("<<x<<","<<y<<","<<z<<")";
}
Vector operator+(Vector v)
{
this->x = this->x + v.x;
this->y = this->y + v.y;
this->z = this->z + v.z;
return *this;
}
bool operator==(Vector v)//overload == operator
{
if(this->x == v.x && this->y == v.y && this->z == v.z)
return true;
else
return false;
}
double length()// calculate length of vector
{
return sqrt(x*x +y*y +z*z);
}
  
};
int main()
{
Vector v1(2.3,5.3,4.2);
Vector v2(4.2,5.4,3.4);
Vector v3(3.8,2.9,4.7);

Vector v4;
cout<<" Sum of v1 and v2 :";
(v1 + v2).display();

if(v1 == v3)
cout<<" Vector v1 and v3 are equal";
else
cout<<" Vector v1 and v3 are not equal";

cout<<" length of vector 1 : "<<v1.length();
cout<<" length of vector 2 : "<<v2.length();
cout<<" length of vector 3 : "<<v3.length();


   return 0;
}

Output:

Sum of v1 and v2 :(6.5,10.7,7.6)
Vector v1 and v3 are not equal
length of vector 1 : 14.6458
length of vector 2 : 7.63937
length of vector 3 : 6.70373