Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Problem B: Vectors, Vectors, Vectors (20 points) This problem will give you practice in writing a problem involving multiple functions, void functions, and pass-by-reference.

C++ image text in transcribed
image text in transcribed
image text in transcribed
Problem B: Vectors, Vectors, Vectors (20 points) This problem will give you practice in writing a problem involving multiple functions, void functions, and pass-by-reference. The vector operations in this problem are often parts of 2 the calculation often done to determine the color of an object for a particular pixel in a graphics scene. Write a program consisting of four functions: . double vectorLength(double u, double v, double w. This function computes and returns the lengtl v of the vector (u, , ) void vectorNormalize(double& u, double v, double w). This function takes a nonzero vector (u, u, w) and normalizes it i.e., it replaces it by a vector in the same direction but with unit length. It should do this by dividing each vector component by the vector length obtained by calling the vectorLength function. e void interpolateVectors (double a, double u, double vi, double w1, double u2, double v2, double w2, double& u, double& v, double& w). This function first computes a vector that is a weighted average of (ui, vi, w) and (u2, 2, u2). Specifically, it should compute the vector It should then normalize it using the vectorNormalize function, and return this mormalized vector through the pass-by-reference parameters u, v, and w int mainO. Your main) program will not be extremely long, but will need to do a number of tasks: Have a user input two vectors. If all components of either vector are zero, print "Vectors must be non-zero" and terminate with exit(1). If the vectors are both non-zero, then your program should do the remaining steps below. Normalize each of the two vectors using the normalize function. Ask the user to input a number n. This will be the umber of "interpolation vectors" to compute, as explained below. The value n should be a positive integer that is at least 2, but you do not need to validate this input. Then call interpolateVectors n times with (u) and (u2, v2, w2) always being the normalized user-input vectors, and a taking on successive, equally spaced values between 0.0 and 1.0, inclusive. For example, if n is 5, then interpolateVectors should be called 5 times with a being 0.0, .25, .5, .75, and 1.0. Each call to interpolateVectors will pass back a vector through the pass-by-reference variables. Your program should print these vectors as shown in the example below: with each value having four digits to the right of the dec- imal point (use cout. setf (ios: :fixed) etc.), and in a field of width 8 (use setw (8)) Here is an example showing the input and output text and formatting. As usual, follow it exactly, and all spaces are single-spaces Input the first vector u1 v1 w1: 1.0 1.0 0.0 Input the second vector u2 v2 w2: 2.0 -1.02.0 Eanter tho number ot intarpolation vectors to comput: Interpolation vectors: Input the first vector u1 v1 w: 1.0 1.0 0.0 Input the second vector u2 v2 w2: 2.0 -1.0 2.0 Enter the number of interpolation vectors to compute: 5 Interpolation vectors: 0.7071 0.7071 0.0000 0.8252 0.5292 0.1973 0.8739 0.2378 0.4241 0.8013-0.0867 0.5920 0.6667-0.3333 0.6667 Here's an example of the output when the input validation fails: Input the first vector u1 vi w1: 1.0 1.0 1.0 Input the second vector u2 v2 w2: 0.00000 Vectors must be non-zero Comments . None of the functions should be especially long. If you find yourself writing large amounts of code, think about whether you are writing the functions efficiently. . Implement your solution incrementally, for example, make sure you get the length function working before beginning to write the normalize function. Then get the normalize function working before writing the interpolate function. (This program has a large number of parts, but should not be extremely difficult if you break the problem up into these subparts, and implement the subparts incrementally.) . Some functions (in particular vectorLength) are very simple. However, make sure you follow the problem instructions here; for example, do not skip writing the length function by doing the length calculation directly in the normalize function

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Which political behavior do I want to improve the most?

Answered: 1 week ago