Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Vectors Definition 1. A vector is formally defined as an element of a vector space. In the commonly encountered vector space R n (i.e., Euclidean

Vectors Definition 1. A vector is formally defined as an element of a vector space. In the commonly encountered vector space R n (i.e., Euclidean n-space), a vector is given by n components and can be specified as hv1, v2, , vni. Vectors are sometimes referred to by the number of coordinates that they have. So a 2-dimensional vector hu1, u2i is often called a two-vector and a 3-dimensional vector hu1, u2, u3i is called a three-vector. More generally, an n-dimensional vector is often called an n-vector. Vectors can be added together (vector addition), subtracted (vector subtraction) and multiplied by scalars (scalar multiplication). Vector multiplication is not uniquely defined, but a number of different types of products, such as the dot product and cross product are commonly defined for vectors.

Operations on Vector Definition 2. Addition/Subtraction: These operations are binary operations on vectors. Two vector can be added or subtracted only if they have the same dimension, number of coordinates. When adding or subtracting two vectors, add or subtract corresponding coordinates.

<1,2,3> + <4,5,6> = <5,7,9>

<1,2,3> - <3,2,1> = <-2,0,2>

Definition 3. Inner product: The inner product, also called scalar product or dot product, of two vectors, denoted U V , where U = and V =

<1,2,3> dot <4,5,6> = <4,10,18>

<1,2,3,-1> dot <3,2,1,5> = <3,4,3,-5>

Definition 4. Cross product: The cross product of two three-dimensional vectors U and V , denoted U V , where U = hu1, u2, , uni and V = hv1, v2, , vni, is U V = hu2v3 u3v2, u1v3 + u3v1, u1v2 u2v1i .

Definition 5. Length: The length of a vector U = hu1, u2, , uni, denoted |U|, is given by the expression |U| = U U = p u 2 1 + u 2 2 + + u 2 n .

Definition 6. Unit Vector: The unit vector of a vector U is a vector whose length is 1 that has the same direction as U. The unit vector of a vector U, denoted Ub, is a vector obtained by dividing each coordinate of U by |U|, the length of U; that is, Ub = D u1 |U| , u2 |U| , , un |U| E

A vector will be represented by an array in dimension-coordinates format: the first element of the array will be the dimension of the vector and the remain elements will be the coordinates of the vector. The number of coordinates will be the same as the dimension of the vector. In other words, an n-dimensional vector will be represented by an array of size n+ 1 whose first element is n. For example, the vector U = h4, 2, 5i will be represented by the array [3, 4, 2, 5]. In this project you will write an interactive program that represents vectors in dimension-coordinates format.

The program will have functions to generate a string representation of a vector in standard form and perform various operations on vectors, including those defined above. In order to effectively manage memory, the array representing the vectors that are entered by the user will be dynamically allocated. Duncan 2 Fall 2018 COPYRIGHT Vector Arithmetic Recall, to dynamically allocate an array, the following syntax may be used:

arrayType* arrayName = new arrayType[arraySize];

Example 1. double* v3 = new double[4];

Dynamically allocating an array gives a programmer the flexibility of being able to create an array of any size during run-time and deallocate the array when it is no longer needed. Unlike static arrays that are deallocated when the function in which they are declared is terminated, dynamically allocated arrays are fully within the programmers control and may be deallocated at any point after they are created. The example above creates an array of doubles that has the size of 4. To deallocate a dynamically allocated array, use the following syntax:

delete[] nameOfArray;

This example shows how the v3 array can be deallocated.

Example 2. delete[] v3;

When an array is deallocated, it is no longer accessible. It has to be reallocated if there is a need to re-use it. Proceed with caution when working with dynamically allocated arrays. When a variable associated with a dynamically allocated array is re-used, be sure to deallocate the array before using the variable. Failure to do so leads to memory leak. Memory leak is the allocation of a chunk of memory that contains inaccessible data and cannot be reallocated. There is a lot more to dynamic memory allocation. For now, this is enough to get us through the project. Write a program called VectorManipulator that uses a modular design. This program will involve the implementation of several functions that use arrays as formal parameters. Some function will return dynamically allocated arrays. Use the to string(double value) function, to convert a double to its string equivalent, and a series of successive concatenations (+) when writing the vectorToString function that returns a string representation of a vector. If a function returns an array of double and is called using parameters that will lead to an indeterminate result, the function will return the NULL constant. For those that return double, when they are called using parameters that will lead to an indeterminate result, the function will return NAN. The NAN constant requires the cmath header file.

In addition to the main, your program should consists of the following functions:

/** *

*Give the dimension of the specified vector

* @param v a vector in dimension-coordinates format

* @return the dimension of this vecot */

int dim(const double v[])

/**

* Gives a string representation of this vector in the form

* , where x1 is the second element of the array,

* x2, the second element of the array, etc and n is the dimension

* of the vector and the first element of the array.

* @param v a dimension-coordinates array representation of a

* vector.

* @return a string representation of the specified vector

*/

string vectToString(const double v[])

/**

* Gives a coordinate with the specified subscript of a vector

* @param v a dimension-coordinates array representation of a

* vector

* @param subscript a subscript of a coordinate of the vector

* @return the specified coordinate of v or NAN when

* subscript < 0 or subscript is greater than the dimension of v

*/ double getCoordinate(const double v[], int subscript)

/**

* Computes the length of this vector

* @param v a dimension-coordinates array representation of a

* vector

* @return the length of the specified vector

*/

double length(const double v[] )

/**

* Determines whether two vectors are equal; two vectors are

* equal if they have the same dimension and their

* corresponding coordinates are equal.

* @param v1 a vector * @param v2 a vector

* @return true if the specified vectors are equal;

* otherwise, false

*/

bool equal(const double v1[], const double v2[])

/**

* Computes the unit vector of the specified vector

* @param v a vector

* @return the unit vector of the specified vector

*/

double* unit(const double v[])

/**

* Computes the sum of the specified vectors

* @param v1 a vector

* @param v2 a vector

* @return the sum of the specified vectors if they have the same

* dimension or NULL, otherwise

*/ double* add(const double v1[], const double v2[])

/**

* Computes the difference of the specified vectors

* @param v1 a vector

* @param v2 a vector

* @return the v1-v2 if the vectors have the same

* dimension or NULL, otherwise

*/

double* sub(const double v1[], const double v2[])

/**

* Computes the dot product of two vectors

* @param v1 a vector

* @param v2 a vector

* @return the dot product of v1 and v2 if they have the same

* dimension; otherwise, NAN

*/

double dot(const double v1[], const double v2[])

/**

* Computes the cross product of two 3-D vectors

* @param v1 a vector * @param v2 a vector

* @return the cross product of v1 and v2 if they are both

* 3-dimensionsional vectors; otherwise, NULL

*/ double* cross(const double v1[], const double v2[])

/**

* Computes the product of a vector and a number (scalar)

* @param v a vector

* @param n a number

* @return a vector whose coordinates are those of v, each

* multiplied by n; that is,

*/ double* mult(const double v[], double n)

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 compute and display v4v5 |v4v5| . 8. Your program will compute the unit vector of v4 v5, v\4 v5.

9. Your program will compute and display v4 (v5 + v6).

10. Your program will compute and display (v4 v5) + (v4 v6).

11. Finally, using the equal function, your program will determine whether or not the vectors calculated in 9 and 10 are equal.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Discuss the steps involved in conducting a task analysis.

Answered: 1 week ago