**Project 3 Sample Solution** [(back to course webpage)](./mcs360_fall2020.html) # Code ```cpp #include #include //for setprecision() #include //for sqrt() struct Vector { double x, y, z; }; Vector generate_vector(double a, double b, double c) { Vector v; v.x = a; v.y = b; v.z = c; return v; } void show(Vector v) { std::cout << std::fixed << std::setprecision(5); std::cout << "<" << v.x << ", " << v.y << ", " << v.z << ">" << std::endl; } Vector normalize(Vector v) { double norm = sqrt(v.x * v.x + v.y * v.y + v.z * v.z); v.x /= norm; v.y /= norm; v.z /= norm; return v; } double dot_product(Vector v1, Vector v2) { return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z; } Vector const_multiply(int c, Vector* p) { // Assuming that we are changing the value of *p since it's called const_multiply() and not const_multiple()... // Not changing the value of *p is also fine and will receive full credit. p->x *= c; p->y *= c; p->z *= c; return *p; } Vector cross_product(Vector* a, Vector* b) { Vector w; w.x = (a->y)*(b->z) - (a->z)*(b->y); w.y = (a->z)*(b->x) - (a->x)*(b->z); w.z = (a->x)*(b->y) - (a->y)*(b->x); return w; } Vector add(Vector* a, Vector* b) { Vector w; w.x = a->x + b->x; w.y = a->y + b->y; w.z = a->z + b->z; return w; } int main() { Vector v1; Vector v2; /*this is how we generate Vectors */ v1 = generate_vector(1,2,3); // [5 Points] for correct generate_vector() function v2 = generate_vector(4,-5,8); /*output format of a Vector*/ // [5 Points] for correct show() function show(v1); // show(v1) prints to screen vector of the form <1.000000, 2.00000, 3.00000> show(v2); // show(v2) prints to screen vector of the form <4.000000, -5.00000, 8.00000> /*make these function calls work in your main function by printing their results to screen. note: these calls are not complete. add the appropriate code/type declaration to produce proper results. in summary, make it work, without changing names of functions */ show(normalize(v1)); // [5 Points] returns the unit vector form of v1 std::cout << "Dot product: " << dot_product(v1, v2) << std::endl; // [5 Points] returns the dot product of v1 and v2 show(const_multiply(5, &v1)); // [10 Points] returns 5*v1. value like 5 is always integer show(cross_product(&v1, &v2)); // [10 Points] returns the cross product of v1, v2 show(add(&v1, &v2)); // [10 Points] returns Vector sum of v1 and v2 return 0; } ``` **Output of above code:** ``` (base) potla@EKR:~/Desktop/mcs360$ ./p3 <1.00000, 2.00000, 3.00000> <4.00000, -5.00000, 8.00000> <0.26726, 0.53452, 0.80178> Dot product: 18.00000 <5.00000, 10.00000, 15.00000> <155.00000, 20.00000, -65.00000> <9.00000, 5.00000, 23.00000> ```