We wish to write a set of C functions to work with 3-D vectors. Write C function
ID: 3590371 • Letter: W
Question
We wish to write a set of C functions to work with 3-D vectors. Write C functions as follows: . VectorMagnitude Input: An array that represents a 3-D vector V = V, V,,VJ -Output. The magnitude of the input vector |V| =/V ++ V? . DotProduct Input: Two arrays of three elements each, that represent vectors Vi and ½ Output!: The dot product of these two vectors h . ½ = ½: * ½2 + VY1 * Vy 2 + V, * Va -Output2: The angle between the two vectors (in degrees) cos-1 Vv 180). Note that this calculation must call the function VectorMagnitude twice . CrossProduct Input: Two arrays of three elements each, that represent vectors Vi and ½ - Outputl: A 3-element array that represents the cross product of the two input vectors. The cross product is described below -Output2: The angle between the two input vectors (in degrees). The method for calculating the angle between the two input vectors, using the cross product is described below The cross product of two vectors is defined as the formal determinant of the following matrix y1 2 1 22 The cross product can be used to find the angle between two vectors 180 sin where V3 × Note that this calculation requires three calls to Vector Magnitude. Also note that the value inside the parenthesis of the sin-function might need limiting between ±1 HINT: Your functions will have to pass arrays back and forth. A pointer argument may also be necessary for the angle between the two vectors. Write a C program that prompts the user to choose between the following options . M for vector magnitude. This option prompts the user to enter in a single 3-D vector . D for dot product. This option prompts the user to enter in two 3-D vectors. . C for cross product. This option prompts the user to enter in two 3-D vectors Print the output of the appropriate calculations to the screenExplanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define PI 3.14159265358979323846
int main()
{
int i;
char op,flag;
float v1[3];
float v2[3];
float v3[3];
float magnitude,dot,angle;
while (1)
{
printf("Enter M to calculate the magnitude of a single 3-D vector ");
printf("Enter D to calculate the dot product of a two 3-D vectors ");
printf("Enter C to calculate the cross product of a two 3-D vectors ");
scanf("%s",&op);
if(op=='m' || op=='M')
{
printf("Enter in Vx Vy Vz (seperated by space) ");
for(i=0;i<3;i++)
scanf("%f",&v1[i]);
magnitude=sqrt((v1[0]*v1[0])+(v1[1]*v1[1])+(v1[2]*v1[2]));
printf("|V| = |<%f, %f, %f>| = %f ",v1[0],v1[1],v1[2],magnitude);
}
else if(op=='d' || op=='D')
{
printf("Calculating the dot product between V1 and V2 ");
printf("Enter in Vx1 Vy1 Vz1 (seperated by space) ");
for(i=0;i<3;i++)
scanf("%f",&v1[i]);
printf("Enter in Vx2 Vy2 Vz2 (seperated by space) ");
for(i=0;i<3;i++)
scanf("%f",&v2[i]);
dot=(v1[0]*v2[0])+(v1[1]*v2[1])+(v1[2]*v2[2]);
angle=(acos(((v1[0]*v2[0])+(v1[1]*v2[1])+(v1[2]*v2[2]))/(sqrt((v1[0]*v1[0])+(v1[1]*v1[1])+(v1[2]*v1[2]))*sqrt((v2[1]*v2[1])+(v2[0]*v2[0])+(v2[2]*v2[2]))))*360)/(PI*2);
printf("V1 dot V2 = %f, theta = %f degrees ",dot,angle);
}
else if(op=='c' || op=='C')
{
printf("Calculating the cross product between V1 and V2 ");
printf("Enter in Vx1 Vy1 Vz1 (seperated by space) ");
for(i=0;i<3;i++)
scanf("%f",&v1[i]);
printf("Enter in Vx2 Vy2 Vz2 (seperated by space) ");
for(i=0;i<3;i++)
scanf("%f",&v2[i]);
v3[0]=(v2[2])*(v1[1])-(v2[1])*(v1[2]);
v3[1]=(v2[0])*(v1[2])-(v2[2])*(v1[0]);
v3[2]=(v2[1])*(v1[0])-(v2[0])*(v1[1]);
angle=(acos(((v1[0]*v2[0])+(v1[1]*v2[1])+(v1[2]*v2[2]))/(sqrt((v1[0]*v1[0])+(v1[1]*v1[1])+(v1[2]*v1[2]))*sqrt((v2[1]*v2[1])+(v2[0]*v2[0])+(v2[2]*v2[2]))))*360)/(PI*2);
printf("V1 X V2 = <%f, %f, %f>, theta = %f ",v3[0],v3[1],v3[2],angle);
}
else
{
printf("Enter a valid Choice ");
continue;
}
printf("Would you like to perform another calculation? (y/n)");
scanf("%s",&flag);
if(flag=='y' || flag=='Y')
continue;
else
break;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.