C Program to Compute Cross Product of Two Vectors

This is a C Program to find the cross product of two vectors. 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 <string.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. const int LOW = 0;
  6. const int HIGH = 10;
  7. int main(int argc, char **argv) {
  8.     time_t seconds;
  9.     time(&seconds);
  10.     srand((unsigned int) seconds);
  11.  
  12.     int u1, u2, u3, v1, v2, v3;
  13.     u1 = rand() % (HIGH - LOW + 1) + LOW;
  14.     u2 = rand() % (HIGH - LOW + 1) + LOW;
  15.     u3 = rand() % (HIGH - LOW + 1) + LOW;
  16.     v1 = rand() % (HIGH - LOW + 1) + LOW;
  17.     v2 = rand() % (HIGH - LOW + 1) + LOW;
  18.     v3 = rand() % (HIGH - LOW + 1) + LOW;
  19.  
  20.     int uvi, uvj, uvk;
  21.     uvi = u2 * v3 - v2 * u3;
  22.     uvj = v1 * u3 - u1 * v3;
  23.     uvk = u1 * v2 - v1 * u2;
  24.  
  25.     printf("The cross product of the 2 vectors \n u = %di + %dj + %dk and \n v = %di + %dj + %dk\n",
  26.             u1, u2, u3, v1, v2, v3);
  27.     printf(" u X v: %di + %dj + %dk", uvi, uvj, uvk);
  28.     return 0;
  29. }

Output:

$ gcc CrossProduct.c
$ ./a.out
 
The cross product of the 2 vectors 
 u = 0i + 0j + 7k and 
 v = 4i + 7j + 2k
 u X v: -49i + 28j + 0k

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

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

If you find any mistake above, kindly email to [email protected]

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 & discussions at Telegram SanfoundryClasses.