VOOZH about

URL: https://www.geeksforgeeks.org/dsa/program-dot-product-cross-product-two-vector/

⇱ Program for dot product and cross product of two vectors - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Program for dot product and cross product of two vectors

Last Updated : 29 Jul, 2024

There are two vector A and B and we have to find the dot product and cross product of two vector array. Dot product is also known as scalar product and cross product also known as vector product.
Dot Product - Let we have given two vector A = a1 * i + a2 * j + a3 * k and B = b1 * i + b2 * j + b3 * k. Where i, j and k are the unit vector along the x, y and z directions. Then dot product is calculated as dot product = a1 * b1 + a2 * b2 + a3 * b3
Example -

A = 3 * i + 5 * j + 4 * k
B = 2 * i + 7 * j + 5 * k
dot product = 3 * 2 + 5 * 7 + 4 * 5
= 6 + 35 + 20
= 61


Cross Product - Let we have given two vector A = a1 * i + a2 * j + a3 * k and B = b1 * i + b2 * j + b3 * k. Then cross product is calculated as cross product = (a2 * b3 - a3 * b2) * i + (a3 * b1 - a1 * b3) * j + (a1 * b2 - a2 * b1) * k, where [(a2 * b3 - a3 * b2), (a3 * b1 - a1 * b3), (a1 * b2 - a2 * b1)] are the coefficient of unit vector along i, j and k directions.
Example -

A = 3 * i + 5 * j + 4 * k
B = 2 * i + 7 * j + 5 * k
cross product
= (5 * 5 - 4 * 7) * i
+ (4 * 2 - 3 * 5) * j + (3 * 7 - 5 * 2) * k
= (-3)*i + (-7)*j + (11)*k


Example -

Input: vect_A[] = {3, -5, 4}
vect_B[] = {2, 6, 5}
Output: Dot product: -4
Cross product = -49 -7 28



Code-


Output
Dot product:-4
Cross product:-49 -7 28

Time Complexity: O(3), the code will run in a constant time because the size of the arrays will be always 3.
Auxiliary Space: O(3), no extra space is required, so it is a constant.


Comment
Article Tags: