Vectors It is common in many Engineering problems to represent quantities using
ID: 3912670 • Letter: V
Question
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ı^+4ȷ^+5k^ m/s" role="presentation">3?^+4?^+5k^ m/s3?^+4?^+5k^ 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 (ı^" role="presentation">?^?^ along the x axis, ȷ^" role="presentation">?^?^ along the y axis, etc.), vector addition would look like this:
C→=A→+B→cxı^+cyȷ^+czk^=(axı^+ayȷ^+azk^)+(bxı^+byȷ^+bzk^)=(ax+bx)ı^+(ay+by)ȷ^+(az+bz)k^" role="presentation">C? cx?^+cy?^+czk^=A? +B? =(ax?^+ay?^+azk^)+(bx?^+by?^+bzk^)=(ax+bx)?^+(ay+by)?^+(az+bz)k^C?=A?+B?cx?^+cy?^+czk^=(ax?^+ay?^+azk^)+(bx?^+by?^+bzk^)=(ax+bx)?^+(ay+by)?^+(az+bz)k^
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 thecross 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:
C→=A→⋅B→cxı^+cyȷ^+czk^=(axı^+ayȷ^+azk^)⋅(bxı^+byȷ^+bzk^)=(ax⋅bx)+(ay⋅by)+(az⋅bz)" role="presentation">C?
. Description Implement a function to caloulate the dot product of two vectors Vectors It is common in many Engineering problems to represent quantities using vectors rather than scalars For example, rather than using calar speed to fepresent lah object's motion (.9 7.02 mal we wi often usea vector de acement instead le g?Si +-4] + 5k m, s). Wo can apply a few importat operationa o ectors, including addition and multiplication. Vector addition To add two wectors together, we ?mply acid ther respective components together. For esample, with the tree amensional oordrete sy termabove i along t'ex axi5, j zulong they 3905, etc ), wector addtion would look like this: This aasumes that tha vectors have the same number of eomponents Vector multiplication tere is more than one way to multiply two vectors The fir? (and 5mplest) 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 is sufficient to know that n is defined 35 the sum of the products of two vectors' oorrespondng componentsThe result is scalar For example, using our three-dimensional example above C-A. B Assignment details For this asaignmant you ed to: 1. complete the contract for the two functions declared below by completing each functions descripthe comment, inclding am necessary pre-condnions, and 2 defne both functions inaC++file caled assign4,cp The two functions are declared in asign4.h (which you can download as a starting poin for your workk this coment vold addvectors( double ai, double b.double c. int length TODO:finish this connent double dotProduct double at double btl, int lengthExplanation / Answer
assign4.cpp
#include<iostream>
# include<conio.h>
using namespace std;
int main ()
{
int i, j, m, n, s, t;
int A[10][10], B[10][10], C[10];
cout << "Enter number of rows and columns of matrix A : ";
cin >> m >> n;
cout << "Enter number of rows and columns of matrix B : ";
cin >> s >> t;
if ((m != s) && (n != t))
{
cout << "Matrix operation requires matrix of same size!";
exit(0);
}
*/ Accept values for matrix A*/
*/ We may use m or s , n or t as the terminating value in for loop. The maximum rows and columns in a matrix*/
cout << "Enter elements of matrix A : ";
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
cin >> A[i][j];
}
}
*/ Accept values for matrix B*/
cout << "Enter elements of matrix B : ";
for (i = 0; i < s; i++)
for (j = 0; j < t; j++)
cin >> B[i][j];
}
}
*/Call Function*/
addVectors(A[],B[],C[],m);
dotProduct(A[],B[],C[],m);
}
/**Vector Addition*/
void addVectors(double a[], double b[],double c[], int length)
{ int i,j;
for (i = 0; i < length; i++)
{
C[i] = 0;
for (j = 0; j < length; j++)
{
C[i] += A[i][j] + B[i][j];
}
printMatrix(C[i],m);
}
/**Vector Product */
double dotProduct(double a[], double b[],double c[], int length)
{
int i,j;
for (i = 0; i < length; i++)
{
C[i] = 0;
for (j = 0; j < length; j++)
C[i] += A[i][j] * B[i][j];
}
// Printing matrix A //
double printMatrix(double matrix[], int k)
{
cout << "Printing of Output Matrix : ";
for (i = 0; i < k; i++)
cout << matrix[i] << " ";
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.