C++ Program to Compute Cross Product of Two Vectors

«
»
This is a C++ Program to find the cross product of two vectors. In mathematics, the cross product or vector product is a binary operation on two vectors in three-dimensional space. It results in a vector that is perpendicular to both and therefore normal to the plane containing them.

Here is source code of the C++ Program to Compute Cross Product of Two Vectors. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include<time.h>
  2. #include<stdlib.h>
  3. #include<iostream>
  4. #include<math.h>
  5.  
  6. using namespace std;
  7. const int LOW = 0;
  8. const int HIGH = 10;
  9. int main(int argc, char **argv)
  10. {
  11.     time_t seconds;
  12.     time(&seconds);
  13.     srand((unsigned int) seconds);
  14.  
  15.     int u1, u2, u3, v1, v2, v3;
  16.     u1 = rand() % (HIGH - LOW + 1) + LOW;
  17.     u2 = rand() % (HIGH - LOW + 1) + LOW;
  18.     u3 = rand() % (HIGH - LOW + 1) + LOW;
  19.     v1 = rand() % (HIGH - LOW + 1) + LOW;
  20.     v2 = rand() % (HIGH - LOW + 1) + LOW;
  21.     v3 = rand() % (HIGH - LOW + 1) + LOW;
  22.  
  23.     int uvi, uvj, uvk;
  24.     uvi = u2 * v3 - v2 * u3;
  25.     uvj = v1 * u3 - u1 * v3;
  26.     uvk = u1 * v2 - v1 * u2;
  27.  
  28.     cout << "The cross product of the 2 vectors \n u = " << u1 << "i + " << u2
  29.             << "j + " << u3 << "k and \n v = " << u1 << "i + " << u2 << "j + "
  30.             << u3 << "k \n ";
  31.     cout << "u X v : " << uvi << "i +" << uvj << "j+ " << uvk << "k ";
  32.     return 0;
  33. }

Output:

advertisement
$ g++ CrossProduct.cpp
$ a.out
 
The cross product of the 2 vectors 
 u = 6i + 9j + 9k and 
 v = 6i + 9j + 9k 
 u X v : 0i +6j+ -6k 
------------------
(program exited with code: 0)
Press return to continue

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement

Here’s the list of Best Books in C++ Programming, Data Structures and Algorithms.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.