- Related Questions & Answers
- C++ Program to Compute Cross Product of Two Vectors
- Program to find out the dot product of two sparse vectors in Python
- C Program for product of array
- Find Maximum dot product of two arrays with insertion of 0's in C++
- Explain the cross product method process in DFA
- Java program to calculate the product of two numbers
- Product of two number using HOC - JavaScript
- Cartesian product of two sets in JavaScript
- Find two distinct prime numbers with given product in C++ Program
- Maximum Product Subarray - Using Two Traversals in C++
- Python Program to Find the Product of two Numbers Using Recursion
- Python Program for Product of unique prime factors of a number
- C++ program to find two numbers with sum and product both same as N
- Find two numbers with sum and product both same as N in C++ Program
- 8086 program to determine product of corresponding elements of two array elements

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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 array.

**What is vector?**

In mathematics, a quantity that has a magnitude and a direction is known as vector whereas a quantity that have only one value as magnitude is known as scalar. The point from where the vector starts is known as 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 NULL vector because in this type of vector initial point and the terminal point is the same.**Coinitial vector −**If two or more vectors have 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?**

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 * b1 – 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

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 2-> 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

#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; }

Dot product:33 Cross product:9 9 18

Advertisements