I get to the second half of the assignment and my brain doesn't seem to want to work, been up for two days straight :P. Anyways I just can't figure out how I can manipulate the arrays for what's needed. Here are the instructions (I'm not trying to get you to do the assignment, just want some tips on what I should do):
Use double as the type for all of your arrays.
Step 1: Write the following five functions that perform various vector operations:
(a) Vector Addition. Two vectors can be added, by adding each of their values component by component. Hence, given two vectors [1,2,3] and [4,5,6], the sum of these vectors is found by adding the first components, then the second, then the third, yielding the answer: [1+4, 2+5, 3+6] = [5, 7, 9]. Note that in order to add two vectors, they must be the same size.
Write a method called add, that takes one integer, and three arrays. The integer specifies the size of the vectors. The first two arrays are the two vectors to be added. The final array is a reference parameter, where the result of the addition will be stored.
Here is an example of some C++ code that uses the add function, as just described.
Code:
double a[] = {1,2,3};
double b[] = {4,5,6};
double c[3];
add(3, a, b, c);
cout << c[0] << ", " << c[1] << ", ", c[2] << endl; // outputs 5, 7, 9
(b) Vector Scaling. A vector can be scaled by a constant value, by multiplying each of the values in the vector to that constant value. Given a vector [1,2,3] and a constant value 5, scaling the vector by that constant is performed by multiplying the constant to the first value, then the second, then the third, yielding the answer [1*5, 2*5, 3*5] = [5,10,15].
Write a method called scale, that takes one integer, one double, and two arrays. The integer specifies the size of the vector. The double value specifies the constant to scale the vector by. The first array is the vector to be scaled. The final array is a refernece parameter, where the result of the scaling will be stored.
Here is an example of some C++ code that uses the scale function, as just described.
Code:
double a[] = {1,2,3};
double c[3];
scale(3, 5.0, a, c);
cout << c[0] << ", " << c[1] << ", ", c[2] << endl; // outputs 5, 10, 15
(c) Dot Product. The dot product of two vectors is found by multiplying each of their values component by component, and summing the resulting values. Given vectors [1,2,3] and [4,5,6] the dot product of the two vectors is (1 * 4) + (2 * 5) + (3 * 6) = 32.
Write a method called dot, that takes one integer, and two arrays. The integer specifies the size of the vector. The two arrays are the two vectors from which to calculate the dot product. The return value of this funtion should be a double value equaling the dot product of the two vectors.
Here is an example of some C++ code that uses the dot function, as just described.
Code:
double a[] = {1,2,3};
double b[] = {4,5,6};
double dp = dot(3, a, b);
cout << "Dot Product = " << dp << endl; // outputs 'Dot Product = 32'
(d) Vector Magnitude. The magnitude of a vector is found by squaring each component in the vector, summing these values, and then taking the square root. Given a vector [1,2,3], the magnitude of this vector is the value sqrt( 1*1 + 2*2 + 3*3 ) = sqrt(14) = 3.74166.
Write a method called magnitude, that takes one integer, and one array. The integer specifies the size of the vector. The array is the vector from which to calculate the magnitude. The return value of this function should be a double value equaling the magnitude of the vector.
Here is an example of some C++ code that uses the magnitude function, as just described.
Code:
double a[] = {1,2,3};
double mag = magnitude(3, a);
cout << "Magnitude = " << mag << endl; // outputs 'Magnitude = 3.74166'
HINT: Use the dot function in your magnitude function. This function can be written with a very small line of code.
(e) Unit Vector. The unit vector is a vector whose magnitude is 1. A vector's corresponding unit vector can be found by dividing each component of the vector by its magnitude (this is also known as normalizing a vector). Given a vector [1,2,3], and recalling that the magnitude of this vector is 3.742, the unit vector is [ 1 / 3.74166, 2 / 3.74166, 3 / 3.74166 ] = [ 0.267261, 0.534522, 0.801784 ].
Write a function called unit, that takes one integer, and two arrays. The integer specifies the size of the vectors. The first array is the vector from which to calculate the unit. The second array is a reference parameter, where the solution will be stored.
Here is an example of some C++ code that uses the unit function, as just described.
Code:
double a[] = {1,2,3};
double c[3];
unit(3, a, c);
cout << c[0] << ", " << c[1] << ", ", c[2] << endl; // outputs 0.267261, 0.534522, 0.801874
Step 2: Complete a program to test your five functions. The program should read two 3-valued vectors, A and B, from the user. The program should then output:
* The sum of A and B.
* A scaled by the value 7.
* The dot product of A and B.
* The magnitude of A.
* The unit vector of A.
NOTE: The 3-valued vectors in your main program are only for testing your functions. Your functions should be able to accommodate arrays of any size, not just of size 3.
Here is an example of the program output:
$./vec
Please enter first vector: 1 2 3
Please enter second vector: 4 5 6
The sum of these two vectors = [5, 7, 9]
Vector A scaled by 7.0 = [7, 14, 21]
The dot product of A and B = 32
The magnitude of A = 3.74166
The unit vector of A = [0.267261, 0.534522, 0.801784]
$