C++ Program for dot product and cross product of two vectors


We are given two vectors let’s say vector A and vector B containing x, y, and directions, and the task is to find the cross product and dot product of the two given vector arrays.

What is a vector?

In mathematics, a quantity that has a magnitude and a direction is known as a vector whereas a quantity that has only one value as magnitude is known as a scalar. The point from where the vector starts is known as the initial point and the point where the vector ends is known as the terminal point. The distance between the initial point and the terminal point of the vector is known as the magnitude of the vector.

There are multiple types of vectors like −

  • Unit vector − A vector whose magnitude is unity which is 1 is known as the unit vector.
  • Zero vector − It is also known as a NULL vector because in this type of vector initial point and the terminal point are the same.
  • Coinitial vector − If two or more vectors have the same initial point or the starting point then they are said to be coinitial vector
  • Collinear vector − if two or more vectors are parallel to the same line then they are said to be collinear vector
  • Equal vector − if two vectors have the same magnitude and direction then they are said to be equal vector

What is Dot Product?

The dot product is also known as the scalar product which is defined as −

Let’s say we have two vectors A = a1 * i + a2 * j + a3 * k and B = b1 * i + b2 * j + b3 * k where i, j and k are the unit vectors which means they have value as 1 and x, y and z are the directions of the vector then dot product or scalar product is equals to 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?

Cross product is also known as the vector product which is defined as −

Let’s say we have two vectors A = a1 * i + a2 * j + a3 * k and B = b1 * i + b2 * j + b3 * k . Then the cross product is equals to (a2 * b3 – a3 * b2) * i - (a1 * b3 – a3 * b1) * j + (a1 * b2 – a2 * b1) * k, where a2 * b3 – a3 * b2, a1 * b3 – a3 * b1 and a1 * b1 – a2 * b1 are the coefficient of unit vector and i, j and k are the directions of the vector.

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

 Live Demo

#include <bits/stdc++.h>
#define size 3
using namespace std;
//function to calculate dot product of two vectors
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;
}
//function to calculate cross product of two vectors
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[] = { 4, 2, -1 };
   int vector_b[] = { 5, 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

Dot product:33
Cross product:9 -9 18

Updated on: 12-Aug-2021

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements