# C Program to do the common vector operations

Vectors is one of the most important topics in mathematics and physical sciences. Here is a C program which can be used to do all the common operations on vectors like dot product, cross product etc,.

## Introduction

Common operations on vectors are:
1) Finding Magnitude of a vector.
2) Finding Direction i.e. angle made by a vector with X, Y, Z axes.
3) Finding angle between two vectors.
4) Finding sum of two vectors.
5) Finding difference of two vectors.
6) Finding product of a scalar and a vector.
7) Finding scalar product of two vectors.
8) Finding vector product of two vectors.
There are some other operations like scalar triple product, vector triple product etc., but they are not commonly used. Using the program below, all the above listed operations can be done.

## Program

`#include<stdio.h>#include<conio.h>#include<math.h>#define RD 57.2957795#define RG 63.6619772typedef long double val;void anglemid_vector(val a,val b,val c,val d,val e,val f,val g,val *h){val i;i=acos((a*d+b*e+c*f)/(sqrt(a*a+b*b+c*c)*sqrt(d*d+e*e+f*f)));if(g!=1&&g!=2&&g!=3)printf("\n SYNTAX ERROR:TRY AGAIN \n ");else*h=(g==1)?(i*RD):((g==2)?(i):(i*RG));}void main(){val choice,a,b,c,d,e,f,g,h,i;for(choice=0;choice<=9;choice++){printf("\n 1.Magnitude(A) ");printf(" 2.Direction(A) ");printf(" 3.Angle(A,B) ");printf(" 4.(A)+(B) ");printf(" 5.(A)-(B) ");printf("\n\n 6.[a]*(A) ");printf(" 7.Scalar product(A,B) ");printf(" 8.Vector product(A,B) ");printf(" 9.Off ");printf("\n\n Enter your choice:");scanf("%Lf",&choice);if(choice==1){printf("\n Enter {I,J,K components} of vector (A):");scanf("%Lf,%Lf,%Lf",&a,&b,&c);d=sqrt(a*a+b*b+c*c);printf("\n Vector (A) is (%Lf)I+(%Lf)J+(%Lf)K \n",a,b,c);printf("\n Magnitude of Vector (A) is {%Lf} \n\n ",d);}else if(choice==2){printf("\n Enter {I,J,K components} of vector (A):");scanf("%Lf,%Lf,%Lf",&a,&b,&c);if(a==0&&b==0&&c==0){printf("\n Vector (A) is (%Lf)I+(%Lf)j+(%Lf)K \n",a,b,c);printf("\n Direction of Vector (A) IS UNDEFINED:TRY AGAIN \n\n ");}else{anglemid_vector(a,b,c,1,0,0,1,&d);anglemid_vector(a,b,c,0,1,0,1,&e);anglemid_vector(a,b,c,0,0,1,1,&f);printf("\n Vector (A) is (%Lf)I+(%Lf)J+(%Lf)K \n",a,b,c);printf("\n Vector (A) makes with [X axis] an angle of {%Lf} \n",d);printf("\n Vector (A) makes with [Y axis] an angle of {%Lf} \n",e);printf("\n Vector (A) makes with [Z axis] an angle of {%Lf} \n\n ",f);}}else if(choice==3){printf("\n Enter {I,J,K components} of vector (A):");scanf("%Lf,%Lf,%Lf",&a,&b,&c);printf("\n Enter {I,J,K components} of vector (B):");scanf("%Lf,%Lf,%Lf",&d,&e,&f);if((a*a+b*b+c*c)!=0&&(d*d+e*e+f*f)!=0){anglemid_vector(a,b,c,d,e,f,1,&g);printf("\n Vector (A) is (%Lf)I+(%Lf)J+(%Lf)K \n",a,b,c);printf("\n Vector (B) is (%Lf)I+(%Lf)J+(%Lf)K \n",d,e,f);printf("\n Angle between vectors (A) and (B) is {%Lf} \n\n ",g);}else{printf("\n Vector (A) is (%Lf)I+(%Lf)J+(%Lf)K \n",a,b,c);printf("\n Vector (B) is (%Lf)I+(%Lf)J+(%Lf)K \n",d,e,f);printf("\n Angle between vectors (A) and (B) IS UNDEFINED:TRY AGAIN \n\n ");}}else if(choice==4){printf("\n Enter {I,J,K components} of vector (A):");scanf("%Lf,%Lf,%Lf",&a,&b,&c);printf("\n Enter {I,J,K components} of vector (B):");scanf("%Lf,%Lf,%Lf",&d,&e,&f);g=a+d;h=b+e;i=c+f;printf("\n Vector (A) is (%Lf)I+(%Lf)J+(%Lf)K \n",a,b,c);printf("\n Vector (B) is (%Lf)I+(%Lf)J+(%Lf)K \n",d,e,f);printf("\n Vector (A)+(B) is {%Lf}I+{%Lf}J+{%Lf}K \n\n ",g,h,i);}else if(choice==5){printf("\n Enter {I,J,K components} of vector (A):");scanf("%Lf,%Lf,%Lf",&a,&b,&c);printf("\n Enter {I,J,K components} of vector (B):");scanf("%Lf,%Lf,%Lf",&d,&e,&f);g=a-d;h=b-e;i=c-f;printf("\n Vector (A) is (%Lf)I+(%Lf)J+(%Lf)K \n",a,b,c);printf("\n Vector (B) is (%Lf)I+(%Lf)J+(%Lf)K \n",d,e,f);printf("\n Vector (A)-(B) is {%Lf}I+{%Lf}J+{%Lf}K \n\n ",g,h,i);}else if(choice==6){printf("\n Enter {a} value:");scanf("%Lf",&a);printf("\n Enter {I,J,K components} of vector (A):");scanf("%Lf,%Lf,%Lf",&b,&c,&d);e=a*b;f=a*c;g=a*d;printf("\n Vector (A) is (%Lf)I+(%Lf)J+(%Lf)K \n",b,c,d);printf("\n Vector [%Lf]*(A) is {%Lf}I+{%Lf}J+{%Lf}K \n\n ",a,e,f,g);}else if(choice==7){printf("\n Enter {I,J,K components} of vector (A):");scanf("%Lf,%Lf,%Lf",&a,&b,&c);printf("\n Enter {I,J,K components} of vector (B):");scanf("%Lf,%Lf,%Lf",&d,&e,&f);g=a*d+b*e+c*f;printf("\n Vector (A) is (%Lf)I+(%Lf)J+(%Lf)K \n",a,b,c);printf("\n Vector (B) is (%Lf)I+(%Lf)J+(%Lf)K \n",d,e,f);printf("\n Scalar product OR Dot product of Vectors \n");printf("\n (A) and (B) represented by (A).(B) is {%Lf} \n\n ",g);}else if(choice==8){printf("\n Enter {I,J,K components} of vector (A):");scanf("%Lf,%Lf,%Lf",&a,&b,&c);printf("\n Enter {I,J,K components} of vector (B):");scanf("%Lf,%Lf,%Lf",&d,&e,&f);g=b*f-c*e;h=c*d-a*f;i=a*e-b*d;printf("\n Vector (A) is (%Lf)I+(%Lf)J+(%Lf)K \n",a,b,c);printf("\n Vector (B) is (%Lf)I+(%Lf)J+(%Lf)K \n",d,e,f);printf("\n Vector product OR Cross product of Vectors (A) and (B) \n");printf("\n represented by (A)x(B) is {%Lf}I+{%Lf}J+{%Lf}K \n\n ",g,h,i);}else if(choice==9){printf("\n ");exit(0);}else{printf("\n {%Lf} IS AN INVALID CHOICE:TRY AGAIN \n\n ",choice);choice=0;}}getch();}`

### Working of the Program

1) Header files stdio.h, conio.h and math.h are used for standard input output operations, conditional input output operations and mathematical operations respectively.
2) Two constants RD and RG are defined which store the conversion factors between degrees, radians and grades.
3) By using the typedef statement, long double can be shortly represented as val.
4) The function anglemid_vector calculates the angle between two vectors in any unit you want.
5) A variable 'choice' is used to select the operation required to be performed on vector(s) and scalar(s).
6) An else-if ladder based on 'choice' contains the code for every operation.
Input vector is scanned as three values i.e. the X, Y and Z components. Correct Output is displayed for correct inputs and Error Message is displayed for error inputs.

In case if you want the code for any one operation, you can select it by knowing the corresponding 'choice' value. Also, new operations can be added very easily.