Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

HINT: Your functions will have to pass arrays back and forth. A pointer argument

ID: 3587387 • Letter: H

Question

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 screen.

2 Vector Calculations 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-VV2 + + » DotProduct Input: Two arrays of three elements each, that represent vectors , and -Output!: The dot product of these two vectors Vi .= ½i * ½2 + *2 + 1 * V,2 v ) 180). Note that this calculation Output2: The angle between the two vectors (in degrees) cos 1 must call the function VectorMagnitude twice CrossProduct Input: Two arrays of three elements each, that represent vectors V1 and ½ -Output 1: 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 U2 y2 The cross product can be used to find the angle between two vectors 180 3 where V's V1 × V Note that this calculation requires three calls to Vector/agnitude. Also note that the value inside the parenthesis of the sin-1 function might need limiting between 1

Explanation / Answer

// Only first four parts and main program have been answered

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define PI 3.14
// function calculates vector magnitude from the x,y and z coordinates passed as an array
float vectorMagnitude(int coordinates[])
{
    //
    return(sqrt(pow(coordinates[0], 2)+ pow(coordinates[1], 2) + pow(coordinates[2], 2)));
}
//
// function computes dot product of two vectors
float dotProduct(int vector1[], int vector2[])
{
    //
    return((vector1[0]*vector2[0]) + (vector1[1]*vector2[1]) + (vector1[2]*vector2[2]));
}
//
// function calculates angle between vectors
float vectorAngleDotProduct(int vector1[], int vector2[])
{
    //
    return (acos((dotProduct(vector1, vector2)) / (vectorMagnitude(vector1) * vectorMagnitude(vector2))) * (180/PI));
}
//
// function computes cross product of two vectors
//
int* crossProduct(int vector1[], int vector2[])
{
    //
    static int crossProductVector[3];
    //
    crossProductVector[0] = (vector1[1]*vector2[2]) - (vector2[1] * vector1[2]); // x3 = y1z2 - y2z1
    //
    crossProductVector[1] = (-1*vector1[0]*vector2[2]) + (vector2[0]*vector1[2]);
    //
    crossProductVector[2] = (vector1[0]*vector2[1]) - (vector2[0]*vector1[1]);
    //
    return(crossProductVector);
    //
}
int main()
{
    //
    float M, D;
    //
    int *C;
    //
    int vector1[3], vector2[3], i;
    //
    char choice = 'X'; // this holds the user choice
    //
    printf("Enter vector x, y, z coordinates of first vector in order : ");
            //
    for(i = 0; i < 3; i++)
    {
        //
        scanf("%d", &vector1[i]);
        //
    }
    //
    printf("Enter vector x, y, z coordinates of second vector in order : ");
    //
    for(i = 0; i < 3; i++)
    {
        //
        scanf("%d", &vector2[i]);
        //
    }
    //
    printf("Enter choice : ");
    //
    printf("M : magnitude, D : dot product, C : cross product, E : Exit ");
    //
    scanf("%c", &choice);
    // loop till user decides to quit
    while(choice != 'E')
    {
        //
        if (choice == 'E')
            break;
        //
        switch(choice)
        {
            //
        case 'M':
            //
            M = vectorMagnitude(vector1);
            //
            printf("Magnitude : %f ", M);
            //
            break;
            //
        case 'D':
            //
            D = dotProduct(vector1, vector2);
            //
            printf("Dot product : %f ", D);
            //
            D = vectorAngleDotProduct(vector1, vector2);
            //
            printf("Angle : %f ", D);
            //
            break;
            //
        case 'C':
            //
            C = crossProduct(vector1, vector2);
            //
            printf("Cross Product vector : ");
            //
            for(i = 0; i < 3; i++)
            {
                //
                printf("%d ",C[i]);
                //
                //C++;
            }

        default:
            break;

        }
        //
        printf("Enter choice : ");
        //
        printf("M : magnitude, D : dot product, C : cross product, E : Exit ");
        //
        scanf("%c", &choice);
    }

    //M = vectorMagnitude(vector1);
    //
    //printf("Hello world! ");
    return 0;
}