Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
VecCrossProd
//g++ 7.4.0 //Modify a Vector class so that its objects are all three a dimensional vectors with subscript ranging from 1 to 3;include addition,scalar multiplication, norm function and a cross-product function //this code is created by Rezaul Hoque on September 16,2021;contact: jewelmrh@yahoo.com //note: codes shared by Rezaul Hoque on rextester are not for sale; they are created and shared to facilitate the algorithm learning process; many like Hoque use this platform to practice programming ;Rezaul hopes his contribution helps others to fine tune their learning; #include <iostream> #include <cmath> class Vector { friend Vector operator*(const Vector&, double t); friend std::ostream& operator<<(std::ostream &, const Vector &);//stream extraction operator friend std::istream& operator>>(std::istream &, Vector &);//stream insertion operator friend Vector operator+(const Vector &, const Vector&);//binary addition operator friend Vector operator-(const Vector &, const Vector&);//binary subtraction operator friend Vector operator&(Vector&, Vector&); public: Vector (int ,double );//default constructor Vector (const Vector &);//copy constructor ~Vector();//destructor const Vector & operator=(const Vector &);//assignment operator double& operator[](int) const;//subscript operator Vector norm(); int size; double * data; }; std::ostream& operator<<(std::ostream& ostr,const Vector& v) { int l; ostr<<"( "; for(l=0;l<v.size-1;l++){ ostr<<v[l]<<","; if((l+1)%8 == 0) std::cout<<"\n"; } return ostr<<v[l]<<")\n"; } std::istream& operator>>(std::istream& istr,const Vector& v) { for(int l=0;l<=v.size-1;l++) { std::cout<<l<<":"; istr>>v[l]; } return istr; } Vector::Vector(int sz=1,double t=0.0): size(sz) { data = new double[size]; for(int l=0;l<size;l++) data[l]=t; } Vector:: Vector(const Vector & v): size(v.size) { data = new double[v.size]; for(int l=0;l<v.size;l++) data[l]=v.data[l]; } Vector::~Vector() { delete [] data; data = NULL; size=0; } const Vector& Vector::operator=(const Vector & v) { if(&v != this) { delete [] data; size = v.size; data = new double[size]; for(int l=0;l<size;l++) data[l]=v.data[l]; } return *this; } Vector operator& (Vector& v,Vector & w) { Vector z(3); z.data[0]=v.data[1]*w.data[2]-v.data[2]*w.data[1]; z.data[1]=v.data[2]*w.data[0]-v.data[0]*w.data[2]; z.data[2]=v.data[0]*w.data[1]-v.data[1]*w.data[0]; return z; } double& Vector:: operator[](int I) const { return data[I]; } Vector operator+(const Vector & v,const Vector& w) { Vector z(3,0.0); for(int l=0;l<z.size;l++){ z.data[l] = v.data[l] + w.data[l];} return z; } Vector operator-(const Vector & v,const Vector& w) { Vector z(3,0.0); for(int l=0;l<z.size;l++){ z.data[l] = v.data[l] - w.data[l];} return z; } Vector operator*(const Vector& v,double b) { Vector z(3); for(int l=0; l<z.size; l++) z.data[l] += v.data[l]*b; return z; } Vector Vector:: norm() { Vector z(3); for(int l=0; l<size; l++) z.data[l] +=sqrt(data[l]*data[l]); return z; } int main() { Vector vec1(3,3.0),vec2(3,4.0),vec3(3),vec4(3); double b=2.0; vec3=vec1+vec2; std::cout<<"Addition between vec1 & vec2:\n"; std::cout<<vec3; std::cout<<"square root of vec2*vec2:\n"<<vec2.norm(); std::cout<<"Cross product of vec1 & vec2:\n"; Vector y(3); y=vec1&vec2; std::cout<<y; vec4=vec1*b; std::cout<<"After multiplying vec1 by 2:\n"; std::cout<<vec4; return 0; }
run
|
edit
|
history
|
help
0
codechef
Mr
Exempel 4
break.cpp
subset sum=k(Recursion)
Counting top students
VecHotel2
DayTempEnum
156
ABC.cpp