Program to calculate angle between two N | C ++ program for the above approach ; Function to find the magnitude of the given vector ; Stores the final magnitude ; Traverse the array ; Return square root of magnitude ; Function to find the dot product of two vectors ; Stores dot product ; Traverse the array ; Return the product ; Stores dot product of two vectors ; Stores magnitude of vector A ; Stores magnitude of vector B ; Stores angle between given vectors ; Print the angle ; Driver Code ; Given magnitude arrays ; Size of the array ; Function call to find the angle between two vectors
#include <bits/stdc++.h> NEW_LINE using namespace std ; double magnitude ( double arr [ ] , int N ) { double magnitude = 0 ; for ( int i = 0 ; i < N ; i ++ ) magnitude += arr [ i ] * arr [ i ] ; return sqrt ( magnitude ) ; } double dotProduct ( double arr [ ] , double brr [ ] , int N ) { double product = 0 ; for ( int i = 0 ; i < N ; i ++ ) product = product + arr [ i ] * brr [ i ] ; return product ; } void angleBetweenVectors ( double arr [ ] , double brr [ ] , int N ) { double dotProductOfVectors = dotProduct ( arr , brr , N ) ; double magnitudeOfA = magnitude ( arr , N ) ; double magnitudeOfB = magnitude ( brr , N ) ; double angle = dotProductOfVectors / ( magnitudeOfA * magnitudeOfB ) ; cout << angle ; } int main ( ) { double arr [ ] = { -0.5 , -2 , 1 } ; double brr [ ] = { -1 , -1 , 0.3 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; angleBetweenVectors ( arr , brr , N ) ; return 0 ; }