Write a program (vector.cpp) that computes the dot product, norm,normalized vect
ID: 3613224 • Letter: W
Question
Write a program(vector.cpp)that computes the dot product, norm,normalized vector and angle between two vectors. Detail`s of theassignment are as follows:
A.Declare two arrays of sizeARRAY_SIZE where ARRAY_SIZE is aconst intvariable set to5.
B.Ask the user to enterthe dcoordinates of the firstvector where dis the vector dimension(here, it is 5). Store the vectorcoordinates in one of the arrays.
C.Ask the user to enterthe dcoordinates of the secondvector where dis the vector dimension.Store the vector coordinates in the second array.
D.Calculate and outputthe dot productbetween the two vectors andthe normof each vector. All outputnumbers should have exactly two digits after the decimalplace.
E.If the norm ofuis zero then output "u: zerolength vector". Similarly, if the norm ofvis zero then output "v: zerolength vector".
F.Calculate and outputthe normalizedvector of eachnon-zeroinput vector. All outputnumbers should have exactly two digits after the decimalplace.
Explanation / Answer
const int ARRAY_SIZE = 5; double u[ARRAY_SIZE]; double v[ARRAY_SIZE]; for(int i=0;i<ARRAY_SIZE;i++) { cout<<"coord"<<i<<"?"<<endl; cin>>u[i]; } for(int i=0;i<ARRAY_SIZE;i++) { cout<<"coord"<<i<<"?"<<endl; cin>>v[i]; } for(int i=0;i<ARRAY_SIZE;i++) { cout<<"coord"<<i<<"?"<<endl; cin>>v[i]; } double dotproduct=0.0f; double normal[2]; double magnitude_u=0.0f; // aka"norm" double magnitude_v=0.0f; // aka"norm" for (int i=0;i<ARRAY_SIZE;i++) {dotproduct+=u[i]*v[i]; magnitude_u+=(u[i]*u[i]); magnitude_v+=(v[i]*v[i]); } cout<<"Dot Product:"<<setprecision(2)<<dotproduct<<endl; cout<<"U Normal :"<<setprecision(2)<<magnitude_u<<endl; cout<<"V Normal :"<<setprecision(2)<<magnitude_v<<endl; if (magnitude_u == 0) cout<<"u: zero vectorlength"<<endl; else norm[0]=sqrt(magnitude_u); // unormalized cout<<setprecision(2)<<"Normalized U:"<<norm[0]<<endl; if (magnitude_v == 0) cout<<"v: zero vectorlength"<<endl; else norm[1]=sqrt(magnitude_v); // unormalized cout<<setprecision(2)<<"Normalized V:"<<norm[1]<<endl;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.