Assignment 4

Important dates

Assigned10 Jul 2018 @ 16:54h
Due17 Jul 2018 @ 23:59h


Implement a function to calculate the dot product of two vectors.


It is common in many Engineering problems to represent quantities using vectors rather than scalars. For example, rather than using a scalar speed to represent an object’s motion (e.g., 7.02 m/s), we will often use a vector displacement instead (e.g., $3 \hat{\imath} + 4 \hat{\jmath} + 5 \hat{k} \textrm{ m/s}$). We can apply a few important operations to vectors, including addition and multiplication.

Vector addition

To add two vectors together, we simply add their respective components together. For example, with the three-dimensional co-ordinate system above ($\hat{\imath}$ along the x axis, $\hat{\jmath}$ along the y axis, etc.), vector addition would look like this:

\[ \begin{align} \vec{C} &= \vec{A} + \vec{B} \\ c_x \hat{\imath} + c_y \hat{\jmath} + c_z \hat{k} &= \left( a_x \hat{\imath} + a_y \hat{\jmath} + a_z \hat{k} \right) + \left( b_x \hat{\imath} + b_y \hat{\jmath} + b_z \hat{k} \right) \\ &= (a_x + b_x) \hat{\imath} + (a_y + b_y) \hat{\jmath} + (a_z + b_z) \hat{k} \end{align} \]

This assumes that the vectors have the same number of components.

Vector multiplication

There is more than one way to multiply two vectors. The first (and simplest!) way is called the dot product (the other way is called the cross product; we may come back to in in the future). The dot product has an important geometric meaning, but for our purposes in this assignment it is sufficient to know that it is defined as the sum of the products of two vectors' corresponding components. The result is a scalar. For example, using our three-dimensional example above:

\[ \begin{align} \vec{C} &= \vec{A} \cdot \vec{B} \\ c_x \hat{\imath} + c_y \hat{\jmath} + c_z \hat{k} &= \left( a_x \hat{\imath} + a_y \hat{\jmath} + a_z \hat{k} \right) \cdot \left( b_x \hat{\imath} + b_y \hat{\jmath} + b_z \hat{k} \right) \\ &= (a_x \cdot b_x) + (a_y \cdot b_y) + (a_z \cdot b_z) \end{align} \]

Assignment details

For this assignment, you need to:

  1. complete the contract for the two functions declared below by completing each function’s descriptive comment, including any necessary pre-conditions, and

  2. define both functions in a C++ file called assign4.cpp.

The two functions are declared in assign4.h (which you can download as a starting point for your work):

 * TODO: finish this comment
void addVectors(double a[], double b[], double c[], int length);

 * TODO: finish this comment
double dotProduct(double a[], double b[], int length);

As always, assignments in this course must be done individually. You can (and are encouraged to!) work together on exercises, but you must do the assignments yourself. If in doubt, come and talk with me.