The objective is to calculate the cross product and dot product of the given arrays of vectors. Consider two vectors, denoted as vector A and vector B, with components along the x, y, and z axes. This guide focuses on developing a C++ script to compute the dot and cross products of these vectors. Prior to delving into the code implementation, it is essential to understand the fundamental concepts of vectors.
What is a vector?
In the field of mathematics, a vector is a quantity that possesses both magnitude and direction, in contrast to a scalar which only has magnitude. The starting point signifies the origin of the vector, while the endpoint indicates its final position. The magnitude of a vector represents the distance between its initial and final points.
There are multiple types of vectors like:
- Unit vector: A vector is said to be a unit vector if its magnitude is unity or 1.
- Zero vector: As the initial point and the terminal point of this kind of vector are the same, it is also referred to as a NULL vector.
- Coinitial vector: Two or more vectors are considered coinitial if they share the same starting point or initial point.
- Collinear vector: Two or more vectors are considered collinear if they run parallel to the same line.
- Equal vector: Two vectors are considered equal if their magnitude and direction are the same.
- The dot product , also known as the inner product or scalar product , is a mathematical operation in which two vectors result in a scalar (a single numeric value). To understand and handle vector behaviors, the dot product is defined for vectors in both 2D and 3D. It is applied in many domains, including as computer graphics, mathematics, and physics
- The dot product is also known as the scalar product, which is defined as:
What is Dot Product?
Suppose we possess two vectors, A = a1 i + a2 j + a3 k and B = b1 i + b2 j + b3 k. These vectors are oriented along the x, y, and z axes, with magnitudes set to 1, signifying their status as unit vectors. The dot product, alternatively referred to as the scalar product, can be calculated as a1 b1 + a2 b2 + a3 * b3.
Input-: A = 2 * i + 7 * j + 2 * k
B = 3 * i + 1 * j + 5 * k
Output-: 2 * 3 + 7 * 1 + 2 * 5 = 23
What is Cross Product?
- In three-dimensional space, the cross product is a binary operation on two vectors. Once more, the outcome is a vector that is orthogonal to both vectors. The right-hand rule can be used to calculate the cross product of two vectors.
- Any two vectors that are perpendicular to the other two vectors yield the right-hand rule. We may also determine the magnitude of the resultant vector by using the cross product.
- The cross product is also known as the vector product, which is defined as:
Assuming that A = a1 i + a2 j + a3 k and B = b1 i + b2 j + b3 k represent two vectors, the cross product is obtained by calculating (a2 b3 - a3 b2) i - (a1 b3 - a3 b1) j + (a1 b2 - a2 b1) k. Here, the unit vector components are derived from the coefficients a2 b3 - a3 b2, a1 b3 - a3 b1, and a1 b2 - a2 * b1 for the directions i, j, and k, respectively.
Input-: A = 2 * i + 7 * j + 2 * k
B = 3 * i + 1 * j + 5 * k
Output-: (7 * 5 - 2 * 1)i + (2 * 5 - 2 * 3)j - (2 * 1 - 7 * 3)k
Algorithm
Start
Step 1 -> Declare a function to calculate the dot product of two vectors
int dot_product(int vector_a[], int vector_b[])
Declare int product = 0
Loop For i = 0 and i < size and i++
Set product = product + vector_a[i] * vector_b[i]
End
return product
Step 2 -> Declare a function to calculate the cross product of two vectors
void cross_product(int vector_a[], int vector_b[], int temp[])
Set temp[0] = vector_a[1] * vector_b[2] - vector_a[2] * vector_b[1]
Set temp[1] = -(vector_a[0] * vector_b[2] - vector_a[2] * vector_b[0])
Set temp[2] = vector_a[0] * vector_b[1] - vector_a[1] * vector_b[0]
Step 3-> In main()
Declare vector int vector_a[] = { 4, 2, -1 }
Declare vector int vector_b[] = { 5, 7, 1 }
Declare variable int temp[size]
Call function for dot product as dot_product(vector_a, vector_b)
Call function for dot product as cross_product(vector_a, vector_b)
Loop For i = 0 and i < size and i++
Print temp[i]
End
Stop
Example:
Let's consider an example to demonstrate the Dot product in C++.
#include <bits/stdc++.h>
#define size 3
using namespace std;
int dot_product(int vector_a[], int vector_b[]) {
int product = 0;
for (int i = 0; i < size; i++)
product = product + vector_a[i] * vector_b[i];
return product;
}
void cross_product(int vector_a[], int vector_b[], int temp[]) {
temp[0] = vector_a[1] * vector_b[2] - vector_a[2] * vector_b[1];
temp[1] = -(vector_a[0] * vector_b[2] - vector_a[2] * vector_b[0]);
temp[2] = vector_a[0] * vector_b[1] - vector_a[1] * vector_b[0];
}
int main() {
int vector_a[] = { 7, 9, 8 };
int vector_b[] = { 1, 7, 1 };
int temp[size];
cout << "Dot product:";
cout << dot_product(vector_a, vector_b) << endl;
cout << "Cross product:";
cross_product(vector_a, vector_b, temp);
for (int i = 0; i < size; i++)
cout << temp[i] << " ";
return 0;
}
Output: