Please answer the following C question:
Read the files vec5C.h, vec5C.c, and main5C.h. Build an executable using gcc -Wall main5C.c vec5C.c
Do this: Add function definitions to vec5C.c so that functions are available for dot product, sum, and cross product. Do not change the function prototypes in vec5C.h. Then add code to main to call these functions and display the dot product of u and v, the sum of u and v, and the cross product of u and v.
File vec5C.h
struct vec {
double x;
double y;
double z;
};
typedef struct vec vec_t;
void print_vec(vec_t v);
// PROMISES:
// Members of v are printed in a format as in this example:
// (-1.25 0.5 19.0)
// REMARK: The interfaces below do not use a consistent style
for
// communication of inputs and results of functions. That would
be
// annoying in production code, but in this exercise it helps
// students learn when to use the . operator and when to use the
->
// operator.
vec_t scalar_product(double k, vec_t v);
// PROMISES:
// Return value is scalar product of k and v.
double dot_product(vec_t left, vec_t right);
// PROMISES:
// Return value is dot product of k and v.
vec_t sum(const vec_t* left, const vec_t *right);
// REQUIRES:
// left and right point to vector objects.
// PROMISES:
// Return value is sum of *left and *right.
void cross_product(const vec_t* left, const vec_t *right, vec_t
*product);
// REQUIRES:
// left, right and product point to vector objects.
// PROMISES:
// *product contains cross product of *left and *right.
File vec5C.c
/ Function definitions for operations on vectors in 3-space.
#include <stdio.h>
#include "vec5C.h"
void print_vec(vec_t v)
{
// REMARK: %g usually does a more helpful job or printing a
double
// than %f does.
printf("(%g %g %g)", v.x, v.y, v.z);
}
vec_t scalar_product(double k, vec_t v)
{
vec_t result;
result.x = k * v.x;
result.y = k * v.y;
result.z = k * v.z;
return result;
}
File main5C.h
// main function to demonstrate operations on vectors in 3-space.
#include <stdio.h>
#include "vec5C.h"
gcc -wall main5c.c vec5c.c
int main(void)
{
vec_t u = {1.5, 0.5, 0.125}, v = {-2.0, 3.5, -3.0};
printf("Value of u: ");
print_vec(u);
printf("\nValue of v: ");
print_vec(v);
printf("\n\n");
// Demonstrate scalar product.
double k = 0.5;
vec_t sp;
sp = scalar_product(k, v);
printf("Scalar product of %g and v is: ", k);
print_vec(sp);
printf("\n\n");
return 0;
}
Include copies of the final .c and .h files and program output.
// vec5C.h
#ifndef VEC5C_H
#define VEC5C_H
struct vec {
double x;
double y;
double z;
};
typedef struct vec vec_t;
void print_vec(vec_t v);
// PROMISES:
// Members of v are printed in a format as in this example:
// (-1.25 0.5 19.0)
// REMARK: The interfaces below do not use a consistent style
for
// communication of inputs and results of functions. That would
be
// annoying in production code, but in this exercise it helps
// students learn when to use the . operator and when to use the
->
// operator.
vec_t scalar_product(double k, vec_t v);
// PROMISES:
// Return value is scalar product of k and v.
double dot_product(vec_t left, vec_t right);
// PROMISES:
// Return value is dot product of k and v.
vec_t sum(const vec_t* left, const vec_t *right);
// REQUIRES:
// left and right point to vector objects.
// PROMISES:
// Return value is sum of *left and *right.
void cross_product(const vec_t* left, const vec_t *right, vec_t
*product);
// REQUIRES:
// left, right and product point to vector objects.
// PROMISES:
// *product contains cross product of *left and *right.
#endif
//end of vec5C.h
// vec5C.c
// Function definitions for operations on vectors in 3-space.
#include <stdio.h>
#include "vec5C.h"
void print_vec(vec_t v)
{
// REMARK: %g usually does a more helpful job or printing a
double
// than %f does.
printf("(%g %g %g)", v.x, v.y, v.z);
}
vec_t scalar_product(double k, vec_t v)
{
vec_t result;
result.x = k * v.x;
result.y = k * v.y;
result.z = k * v.z;
return result;
}
// function to return dot product of left and right
vectors
double dot_product(vec_t left, vec_t right)
{
return (left.x*right.x + left.y + right.y + left.z*right.z);
}
// function to calculate sum of vectors left and right and
return the resultant vector
vec_t sum(const vec_t* left, const vec_t *right)
{
vec_t result;
result.x = left->x + right->x;
result.y = left->y + right->y;
result.z = left->z + right->z;
return result;
}
// function to calculate cross product of left and right and
store it in product
void cross_product(const vec_t* left, const vec_t *right, vec_t
*product)
{
product->x =
(left->y*right->z)-(left->z*right->y);
product->y =
(left->z*right->x)-(left->x*right->z);
product->z =
(left->x*right->y)-(left->y*right->x);
}
// end of vec5C.c
// main5C.c : main function to demonstrate operations on vectors in 3-space.
#include <stdio.h>
#include "vec5C.h"
int main(void)
{
vec_t u = {1.5, 0.5, 0.125}, v = {-2.0, 3.5, -3.0};
printf("Value of u: ");
print_vec(u);
printf("\nValue of v: ");
print_vec(v);
printf("\n\n");
// Demonstrate scalar product.
double k = 0.5;
vec_t sp;
sp = scalar_product(k, v);
printf("Scalar product of %g and v is: ", k);
print_vec(sp);
printf("\n\n");
printf("Dot product of u and v is: %f",dot_product(u,v));
printf("\n\n");
vec_t result = sum(&u,&v);
printf("Sum of u and v is: ");
print_vec(result);
printf("\n\n");
cross_product(&u, &v, &result);
printf("Cross product of u and v is: ");
print_vec(result);
printf("\n\n");
return 0;
}
// end of main5C.c
Output:
Get Answers For Free
Most questions answered within 1 hours.