Question

In: Computer Science

C++ please test 1 > run Enter 1st vector (2 floats): 0 0 Coordinates cannot both...

C++ please 
test 1
> run
Enter 1st vector (2 floats): 0 0
Coordinates cannot both be zero.
Enter 1st vector (2 floats): 0 0
Coordinates cannot both be zero.
Enter 1st vector (2 floats): 1 0
Enter 2nd vector (2 floats): 0 0
Coordinates cannot both be zero.
Enter 2nd vector (2 floats): 0 0
Coordinates cannot both be zero.
Enter 2nd vector (2 floats): 1 0

First vector: (1, 0) has length 1
Second vector: (1, 0) has length 1
Angle between vectors (1, 0) and (1, 0) = 0 degrees.
The vectors are NOT ORTHOGONAL.

Test 2
> run
Enter 1st vector (2 floats): 1 0
Enter 2nd vector (2 floats): 0 0
Coordinates cannot both be zero.
Enter 2nd vector (2 floats): 1 1

First vector: (1, 0) has length 1
Second vector: (1, 1) has length 1.41421
Angle between vectors (1, 0) and (1, 1) = 45 degrees.
The vectors are NOT ORTHOGONAL.

Test 3
> run
Enter 1st vector (2 floats): 0 1
Enter 2nd vector (2 floats): 1 0

First vector: (0, 1) has length 1
Second vector: (1, 0) has length 1
Angle between vectors (0, 1) and (1, 0) = 90 degrees.
The vectors are ORTHONORMAL.

Test 4
> run
Enter 1st vector (2 floats): 2 2
Enter 2nd vector (2 floats): -1 0

First vector: (2, 2) has length 2.82843
Second vector: (-1, 0) has length 1
Angle between vectors (2, 2) and (-1, 0) = 135 degrees.
The vectors are NOT ORTHOGONAL.

Test 5
> run
Enter 1st vector (2 floats): 1.25 -0.25
Enter 2nd vector (2 floats): 0.35 3.14

First vector: (1.25, -0.25) has length 1.27475
Second vector: (0.35, 3.14) has length 3.15945
Angle between vectors (1.25, -0.25) and (0.35, 3.14) = 94.9497 degrees.
The vectors are NOT ORTHOGONAL.

Test 6
> run
Enter 1st vector (2 floats): 0.35 3.14
Enter 2nd vector (2 floats): 1.25 -0.25

First vector: (0.35, 3.14) has length 3.15945
Second vector: (1.25, -0.25) has length 1.27475
Angle between vectors (0.35, 3.14) and (1.25, -0.25) = 94.9497 degrees.
The vectors are NOT ORTHOGONAL.78

Test 7
> run
Enter 1st vector (2 floats): 0.70710678118 0.70710678118
Enter 2nd vector (2 floats): -0.70710678118 0.70710678118

First vector: (0.707107, 0.707107) has length 1
Second vector: (-0.707107, 0.707107) has length 1
Angle between vectors (0.707107, 0.707107) and (-0.707107, 0.707107) = 90 degrees.
The vectors are ORTHONORMAL.

Test 8
> run
Enter 1st vector (2 floats): -0.70710678118 0.70710678118
Enter 2nd vector (2 floats): -0.70710678118 -0.70710678118

First vector: (-0.707107, 0.707107) has length 1
Second vector: (-0.707107, -0.707107) has length 1
Angle between vectors (-0.707107, 0.707107) and (-0.707107, -0.707107) = 90 degrees.
The vectors are ORTHONORMAL.

Test 9

> run
Enter 1st vector (2 floats): 0 -5
Enter 2nd vector (2 floats): -1 0

First vector: (0, -5) has length 5
Second vector: (-1, 0) has length 1
Angle between vectors (0, -5) and (-1, 0) = 90 degrees.
The vectors are ORTHOGONAL, but NOT ORTHONORMAL.

A 2D vector consists of an (x, y) pair in the cartesian coordinate system. Write a program that reads in a pair of 2D vectors and outputs the angle between the vectors in degrees and whether they are orthogonal, orthonormal, or orthogonal but not orthonormal. Please read the description of the math required in this assignment at this link https://www.mathsisfun.com/algebra/vectors-dot-product.html. Your task is to add eight functions so that the program produces the desired results.

Use the global constant called EPSILON whenever you are comparing two decimal numbers (type double). See the lecture notes "13_Numerical" on Numerical Error.

For each function below, first write the function prototype before the main function and then write the function definition after the main function. The comments in the program template indicate where to place these.

TASK 3: Write a function called read_vector which inputs from the user a 2D vector by reading its x- and y- coordinates. If the user enters zero for BOTH coordinates then repeatedly display the message "Coordinates cannot both be zero." and re-prompt the user. Use the global constant EPSILON to compare these values to zero. Define the function so that it does not return a value and has three input parameters:

  • Type string that holds the prompt displayed to the user. Define the string as a const parameter and use pass-by-reference. See the lecture notes on how to do this.
  • Type double that holds the first coordinate of the 2D vector. Define this parameter as pass-by-reference.
  • Type double that holds the second coordinate of the 2D vector. Define this parameter as pass-by-reference.

TASK 4: Write a function called compute_vector_angle which computes the angle between two vectors (see the math in the link provided above) in RADIANS given two vectors. This function will call two functions (see TASK 4a and TASK 4c), called normalize and dot_product. Define the function compute_vector_angle so that it returns the angle in RADIANS (type double) and has four input parameters (all pass-by-value):

  • Type double that holds the x coordinate of the first vector.
  • Type double that holds the y coordinate of the first vector.
  • Type double that holds the x coordinate of the second vector.
  • Type double that holds the y coordinate of the second vector.

As described in https://www.mathsisfun.com/algebra/vectors-dot-product.html, the angle between two vectors can be computed using the dot product. If you want to compute the angle between the vectors a and b, then use a · b = |a| * |b| * cos(θ) and solve for θ, where |a| and |b| are the lengths of the vectors. To simplify this calculation, we can use the normalized versions of vectors a and b. A normalized vector has the same direction but instead has unit length. If we normalize the vectors a and b into two new vectors, say a' and b', then a' · b' = cos(θ), which has the same angle θ in the equation for a · b. If you solve for θ then you have computed the angle between the vectors a and b. I.e., θ = arc cos(a' · b').

The algorithm for computing the angle is:

  1. Normalize (x1, y1) and (x2, y2).
  2. Compute the dot product between the normalized vectors.
  3. Return the arc cosine of the dot product.

TASK 4a: Write a function called normalize which normalizes a vector (x, y) by dividing by its length. This function is called twice by the compute_vector_angle function. Call the function length_vec in TASK 4b to compute the length of a vector. Define the function normalize so that it does not return a value and has two parameters (both pass-by-reference):

  • Type double that holds the x coordinate of the vector.
  • Type double that holds the y coordinate of the vector.

Given vector (x, y) and its length l, the normalized vector is (x', y') where x' = x/l and y' = y/l. Though, if length l is zero then x' = 0 and 'y' = 0. Use the global constant EPSILON to compare the length value to zero.

TASK 4b: Write a function called length_vec which computes the length of vector (x, y) as √ (x2 + y2), i.e. the square root of (x2 + y2). Define the function length_vec so that it returns the length (type double) and has two input parameters (both const and pass-by-value):

  • Type double that holds the x coordinate of the first vector.
  • Type double that holds the y coordinate of the first vector.

TASK 4c: Write a function called dot_product which computes the dot product of two vectors, say (x1, y1) and (x2, y2), as x1 * x2 + y1 * y2 (see https://www.mathsisfun.com/algebra/vectors-dot-product.html). Define the function dot_product so that it returns the dot product (type double) and has four input parameters (all const and pass-by-value):

  • Type double that holds the x coordinate of the first vector.
  • Type double that holds the y coordinate of the first vector.
  • Type double that holds the x coordinate of the second vector.
  • Type double that holds the y coordinate of the second vector.

TASK 5: Write a function called radians2degrees which converts radians to degrees. See the lecture notes on how to compute this. Define the function radians2degrees so that it returns a value in degrees (type double) and has one input parameter (const and pass-by-value):

  • Type double that holds a value in radians.

TASK 6: Write a function called output_angle which displays both input vectors, the angle between the two vectors, and whether the vectors are either "NOT ORTHOGONAL", "ORTHONORMAL" ,or "ORTHOGONAL, but NOT ORTHONORMAL". See the test runs above. This function will call the function print_vec (see TASK 6a) twice. Define the function so that it does not return a value and has five input parameters (all const and pass-by-value):

  • Type double that holds the x coordinate of the first vector.
  • Type double that holds the y coordinate of the first vector.
  • Type double that holds the x coordinate of the second vector.
  • Type double that holds the y coordinate of the second vector.
  • Type double that holds the angle between the two vectors.

Two vectors are ORTHOGONAL if the angle between them is 90 degrees. Two vectors are ORTHONORMAL if they are orthogonal and both have unit length (the vector length is one). Two vectors are ORTHOGONAL, BUT NOT ORTHONORMAL when they are orthogonal but at least one vector does not have unit length. To check for orthgonality, use the global constant EPSILON to compare the angle with 90 degrees. To check that a vector has unit length, use the global constant EPSILON to compare its length with one. Compute the vector's length by calling the function length_vec (see TASK 4b).

TASK 6a: Write a function called print_vec which displays a message, the coordinates of a vector, and its length. Define the function so that it does not return a value and has four input parameters:

  • Type string that holds a message to display. Define the string as a const parameter and use pass-by-reference.
  • Type double that holds the first coordinate of a 2D vector. Define this parameter as const and pass-by-value.
  • Type double that holds the second coordinate of a 2D vector. Define this parameter as const and pass-by-value.
  • Type double that holds the length of a 2D vector. Define this parameter as const and pass-by-value.

Solutions

Expert Solution

Program

#include<iostream>
#include<cmath>

using namespace std;

const double EPSILON = 1e-11;

// function prototypes
void read_vector(string, double&, double&);
double length_vec(double, double);
double dot_product(double,double,double,double);
double compute_vector_angle(double,double,double,double);
double radians2degrees(double);
void normalize(double &u, double &v);
void print_vec(string, double, double);
void output_angle(double,double,double,double,double);

// *** DO NOT CHANGE ANY CODE IN THE MAIN FUNCTION.
int main()
{
double u1, v1; // coordinates of vector 1
double u2, v2; // coordinates of vector 2
double radians; // angle in radians
double degrees; // angle in degrees

read_vector("Enter 1st vector (2 floats): ", u1, v1);
read_vector("Enter 2nd vector (2 floats): ", u2, v2);

// compute angle in radians between (u1, v1) and (u2, v2)
radians = compute_vector_angle(u1, v1, u2, v2);

// convert radians to degrees
degrees = radians2degrees(radians);

// output angle
output_angle(u1, v1, u2, v2, degrees);

cout << endl;

return(0);
}

// DEFINE FUNCTION read_vector
void read_vector(string msg, double &u, double &v)
{
while(1)
{
cout<<msg;
cin>>u>>v;
if(u!=0 || v!=0)
return;
cout<<"Coordinates cannot both be zero."<<endl;
}
}

// DEFINE FUNCTION length_vec
double length_vec(double u, double v)
{
return sqrt(u*u+v*v);
}

// DEFINE FUNCTION dot_product
double dot_product(double u1, double v1, double u2, double v2)
{
return u1*u2 + v1*v2;
}

// DEFINE FUNCTION compute_vector_angle HERE.
double compute_vector_angle(double u1, double v1, double u2, double v2)
{
double len1 = length_vec(u1, v1);
double len2 = length_vec(u2, v2);
normalize(u1, v1);
normalize(u2, v2);
double prod = dot_product(u1, v1, u2, v2);

double angle = acos(prod);

return angle;
}

//DEFINE FUNCTION radians2degrees
double radians2degrees(double rad)
{
double pi = 4*atan(1);
return 180*rad/pi;
}


// DEFINE FUNCTION normalize
void normalize(double &u, double &v)
{
double len = length_vec(u, v);
u = u/len;
v = v/len;
}

// DEFINE FUNCTION print_vec
void print_vec(string msg, double u, double v)
{
cout<<msg<<"("<<u<<","<<v<<") has length "<<length_vec(u, v)<<endl;
}

// DEFINE FUNCTION output_angle
void output_angle(double u1, double v1, double u2, double v2, double degrees)
{
print_vec("First vector:", u1, v1);
print_vec("Second vector:", u2, v2);
cout<<"Angle between vectors "<<"("<<u1<<","<<v1<<") and ("<<u2<<","<<v2<<") = "<<degrees <<" degrees."<<endl;

if(fabs(degrees-90)<=EPSILON && fabs(length_vec(u1, v1)-1)<=EPSILON && fabs(length_vec(u2, v2)-1)<=EPSILON)
cout<<"The vectors are ORTHONORMAL."<<endl;
else if(fabs(degrees-90)<=EPSILON && (fabs(length_vec(u1, v1)-1)>EPSILON|| fabs(length_vec(u2, v2)-1)>EPSILON))
cout<<"The vectors are ORTHOGONAL, but NOT ORTHONORMAL."<<endl;
else if(fabs(degrees-90)<=EPSILON)
cout<<"The vectors are ORTHOGONAL."<<endl;
else
cout<<"The vectors are NOT ORTHOGONAL."<<endl;
}

Output:

Solving your question and helping you to well understand it is my focus. So if you face any difficulties regarding this please let me know through the comments. I will try my best to assist you. However if you are satisfied with the answer please don't forget to give your feedback. Your feedback is very precious to us, so don't give negative feedback without showing proper reason.
Always avoid copying from existing answers to avoid plagiarism.
Thank you.


Related Solutions

STL vector C++ /* ---- OUTPUT ---- Enter test scores (-1 to quit) Enter a score:  77...
STL vector C++ /* ---- OUTPUT ---- Enter test scores (-1 to quit) Enter a score:  77 Enter a score:  83 Enter a score:  99 Enter a score:  67 Enter a score:  88 Enter a score:  -1 The average score is 82.8. Write a program in C++ that prompts the user to   Enter test scores (-1 to quit)    (see output) The program calculates and displays   the average of the test scores. Use a vector to hold the values double data type Pass the vector to a...
urgent!!! code in c++ - cannot use vector - please use inheritance -please identify the .h...
urgent!!! code in c++ - cannot use vector - please use inheritance -please identify the .h and .cpp files and add tester program and screenshot of output! -please complete all parts I will upvote thank you!!! Define the following classes to manage the booking of patients in a medical clinic. a) Define a class Date that has the following integer data members: month, day and year. b) Define a class AppointmentTime that has the following data members: day (string), hour...
urgent!!! code in c++ - cannot use vector - please use inheritance -please identify the .h...
urgent!!! code in c++ - cannot use vector - please use inheritance -please identify the .h and .cpp files and add tester program and screenshot of output! -please complete all parts I will upvote thank you!!! Define the following classes to manage the booking of patients in a medical clinic. a) Define a class Date that has the following integer data members: month, day and year. b) Define a class AppointmentTime that has the following data members: day (string), hour...
Let x3 be the following vector: x3 <- c(0, 1, 1, 2, 2, 2, 3, 3,...
Let x3 be the following vector: x3 <- c(0, 1, 1, 2, 2, 2, 3, 3, 4) Imagine what a histogram of x3 would look like. Assume that the histogram has a bin width of 1. How many bars will the histogram have? Where will they appear? How high will each be? When you are done, plot a histogram of x3 with bin width = 1, and see if you are right. I need code help R programming
A non-uniform b-spline curve knot vector is given as (-1, -1, -1, -1/2, 0, 0, 0,...
A non-uniform b-spline curve knot vector is given as (-1, -1, -1, -1/2, 0, 0, 0, 1, 1, 1, 3/2, 2, 2, 3, 3, 3, 3). Show the equation diagrammatically and sketch the curve with their respective control polygons if the curve is cubic (degree 3, order 4).
?" + 3?′ + 2? = ????, ?(0) = 0, ?′(0) = 2 1) Please solve...
?" + 3?′ + 2? = ????, ?(0) = 0, ?′(0) = 2 1) Please solve using an annihilator 2) Please solve using the Method of Variation of Parameters Thank you.
What is the sum of the probabilities of all outcomes in a probability distribution? a. 0 b. 1/2 c. 1 d. It cannot be determined.
What is the sum of the probabilities of all outcomes in a probability distribution?a. 0b. 1/2c. 1d. It cannot be determined.
(1) (2) (3) DI C DI C DI C $0 $4 $0 $65 $0 $2 10...
(1) (2) (3) DI C DI C DI C $0 $4 $0 $65 $0 $2 10 11 80 125 20 20 20 18 160 185 40 38 30 25 240 245 60 56 40 32 320 305 80 74 50 39 400 365 100 92 Refer to the given consumption schedules. DI signifies disposable income and C represents consumption expenditures. All figures are in billions of dollars. At an income level of $40 billion, the average propensity to consume is...
Find the coordinates of the orthocenter of the triangle whose vertices are A(3, 1), B(0, 4) and C(-3, 1).
Find the coordinates of the orthocenter of the triangle whose vertices are A(3, 1), B(0, 4) and C(-3, 1).
Let C be the following matrix: C=( 1 2 3 -2 0 1 1 -2 -1...
Let C be the following matrix: C=( 1 2 3 -2 0 1 1 -2 -1 3 2 -8 -1 -2 -3 2 ) Give a basis for the row space of Cin the format [1,2,3],[3,4,5], for example.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT