i was trying to write a program that computes the dot product,norm, normalized v
ID: 3613065 • Letter: I
Question
i was trying to write a program that computes the dot product,norm, normalized vector between two vectors. A) Declare two arrays of size Array_size where Array_size is aconst int varable set to 5. B) Ask the user to enter the d coordinates of the first vectorwhere d is the vector dimension (here it is 5). store the vectorcoordinates in one of the arrays. C) Ask the user to enter the d coordinates of the secondvector where d is the vector dimension. store the vectorcoordinates in one of the arrays. D) Calculate and output the dot product between the twovectors and the norm of each vector. all output numbers should haveexactly two digits after the decimal place. E) If the norm of u is zero then output "u: zero lengthvector". similarly if the norm of v is zero then output "v: zerolength vector" F) Calculateand ouptut the nomalized vector of each non-zeroinput vector. all output numbers should have exactly two digitsafter the decimal place. i was trying to write a program that computes the dot product,norm, normalized vector between two vectors. A) Declare two arrays of size Array_size where Array_size is aconst int varable set to 5. B) Ask the user to enter the d coordinates of the first vectorwhere d is the vector dimension (here it is 5). store the vectorcoordinates in one of the arrays. C) Ask the user to enter the d coordinates of the secondvector where d is the vector dimension. store the vectorcoordinates in one of the arrays. D) Calculate and output the dot product between the twovectors and the norm of each vector. all output numbers should haveexactly two digits after the decimal place. E) If the norm of u is zero then output "u: zero lengthvector". similarly if the norm of v is zero then output "v: zerolength vector" F) Calculateand ouptut the nomalized vector of each non-zeroinput vector. all output numbers should have exactly two digitsafter the decimal place.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
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.