42.3k views
4 votes
The Main function Write a main that performs that tasks below. A simplifying assumption is that all user inputs are valid and that all the expressions are defined. 1. Your program will prompt the user for the dimension and coordinates of three vectors, say v1, v2, and v3. Your program will then dynamically allocate three arrays to represent the three vectors as three arrays in dimension-coordinates format using these values entered by the user. 2. Your program will then display v1, v2, and v3. 3. Your program will compute and display v1 + v2. 4. Your program will compute and display (v2 + v3) • v1 and |v1 − v3|. 5. Your program will compute and display v1 • v2 and |v2| |v1|. 6. Your program will prompt the user for three 3-dimensional vectors, say v4, v5, and v6. Your program will then display these vectors. Using these vectors, your program will compute and display the remaining vector expressions. 7. Your program will determine whether v4 •(v5+v6) and (v4 •v5)+(v4 •v6) are equal using ’=’ to indicate that they are equal and ’<>’ to denote that they are not equal. 8. Your program will compute and display v4+v5 |v4+v5| . 9. Your program will compute and display v5 |v5+v6| + v6 |v5+v6| . 10. Finally, using the equal function, your program will determine whether or not the vectors calculated in 8 and 9 are equal using ’=’ to indicate that they are equal and ’<>’ to denote that they are not equal.

1 Answer

4 votes

Answer:

Step-by-step explanation:

#include <iostream>

#include <math.h>

using namespace std;

int equal(float a[3], float b[3])

{

for(int i=0;i<3;i++)

if(a[i]!=b[i])

return 0;

return 1;

}

int main()

{

int dimension;

cout<<"Enter the dimension of three vectors ->";

cin>>dimension;

float *v1=new float[dimension];

float *v2=new float[dimension];

float *v3=new float[dimension];

cout<<"Enter the coordinates of v1 ->";

for(int i=0;i<dimension;i++)

{cin>>v1[i];

}

cout<<"Enter the coordinates of v2 ->";

for(int i=0;i<dimension;i++)

{cin>>v2[i];

}

cout<<"Enter the coordinates of v3 ->";

for(int i=0;i<dimension;i++)

{cin>>v3[i];

}

cout<<"v1 = < ";

for(int i=0;i<dimension;i++)

cout<<v1[i]<<" ";

cout<<">\\";

cout<<"v2 = < ";

for(int i=0;i<dimension;i++)

cout<<v2[i]<<" ";

cout<<">\\";

cout<<"v3 = < ";

for(int i=0;i<dimension;i++)

cout<<v3[i]<<" ";

cout<<">\\";

cout<<"v1+v2= < ";

for(int i=0;i<dimension;i++)

cout<<v1[i]+v2[i]<<" ";

cout<<">\\";

cout<<"(v2+v3)*v1 = ";

int sum=0;

for(int i=0;i<dimension;i++)

{

sum+=((v2[i]+v3[i])*v1[i]);

}

cout<<sum;

cout<<"\\";

cout<<"|v1-v3| = ";

sum=0;

for(int i=0;i<dimension;i++)

{

sum+=((v1[i]-v3[i])*(v1[i]-v3[i]));

}

cout<<sqrt(sum);

cout<<"\\";

cout<<"v1*v2 = ";

sum=0;

for(int i=0;i<dimension;i++)

{

sum+=(v2[i]*v1[i]);

}

cout<<sum;

cout<<" and ";

cout<<"|v2|*|v1| = ";

int sum1=0,sum2=0;

for(int i=0;i<dimension;i++)

{

sum1+=((v2[i])*v2[i]);

sum2+=((v1[i])*v1[i]);

}

cout<<sqrt(sum1*sum2);

cout<<"\\";

cout<<"\\Enter three 3-D vectors.";

dimension=3;

float *v4=new float[dimension];

float *v5=new float[dimension];

float *v6=new float[dimension];

cout<<"\\Enter v4 ->";

for(int i=0;i<dimension;i++)

{cin>>v4[i];

}

cout<<"Enter v5 ->";

for(int i=0;i<dimension;i++)

{cin>>v5[i];

}

cout<<"Enter v6 ->";

for(int i=0;i<dimension;i++)

{cin>>v6[i];

}

cout<<"v4 = < ";

for(int i=0;i<dimension;i++)

cout<<v4[i]<<" ";

cout<<">\\";

cout<<"v5 = < ";

for(int i=0;i<dimension;i++)

cout<<v5[i]<<" ";

cout<<">\\";

cout<<"v6 = < ";

for(int i=0;i<dimension;i++)

cout<<v6[i]<<" ";

cout<<">\\";

//compute v4xv5

float tem[3];

tem[0]=v4[1]*v5[2]-v4[2]*v5[1];

tem[1]=v4[2]*v5[0]-v4[0]*v5[2];

tem[2]=v4[0]*v5[1]-v4[1]*v5[0];

float mag=0;

for(int i=0;i<3;i++)

mag+=(tem[i]*tem[i]);

mag=sqrt(mag);

cout<<"tem = < ";

for(int i=0;i<dimension;i++)

cout<<tem[i]<<" ";

cout<<">\\";

cout<<mag<<"\\";

cout<<"v4xv5/(|v4xv5|) = <";

for(int i=0;i<3;i++)

cout<<tem[i]/mag<<" ";

cout<<" >\\";

cout<<"unit(v4xv5) = <";

for(int i=0;i<3;i++)

cout<<tem[i]/mag<<" ";

cout<<" >\\";

float v7[3];

v7[0]=v5[0]+v6[0];

v7[1]=v5[1]+v6[1];

v7[2]=v5[2]+v6[2];

tem[0]=v4[1]*v7[2]-v4[2]*v7[1];

tem[1]=v4[2]*v7[0]-v4[0]*v7[2];

tem[2]=v4[0]*v7[1]-v4[1]*v7[0];

cout<<"(v4x(v5+v6)) = < ";

for(int i=0;i<3;i++)

cout<<tem[i]<<" ";

cout<<" >\\";

float tem2[3];

tem2[0]=v4[1]*v5[2]-v4[2]*v5[1] + v4[1]*v6[2]-v4[2]*v6[1];

tem2[1]=v4[2]*v5[0]-v4[0]*v5[2] + v4[2]*v6[0]-v4[0]*v6[2];

tem2[2]=v4[0]*v5[1]-v4[1]*v5[0] + v4[0]*v6[1]-v4[1]*v6[0];

cout<<"(v4 x v5)+(v4 x v6) = < ";

for(int i=0;i<3;i++)

cout<<tem2[i]<<" ";

cout<<" >\\";

if(equal(tem,tem2))

cout<<"v4x(v5+v6) and (v4+v5)+(v4xv6) are equal";

else

cout<<"v4x(v5+v6) and (v4+v5)+(v4xv6) are not equal";

return 0;

}

User Absurd
by
6.2k points