In: Computer Science
C++
Suppose the vector v = [4 -6 7]. Create three vectors:
1. p, which is twice as long as v and points in the same
direction as v
2. q, which has the same length as v and points in the opposite
direction of v
3. r, which is three quarters the length of v and points in the
same direction as v
Print out the results of each vector calculation.
#include <iostream>
using namespace std;
void printVector(float vector[]) {
for (int i = 0; i < 3; i++)
cout << vector[i] << " ";
cout << endl;
}
void multiplyVector(float vector[], float multiplier) {
float* p = new float[3];
for (int i = 0; i < 3; i++)
p[i] = vector[i] * multiplier;
printVector(p);
}
void doubleVector(float vector[]) {
multiplyVector(vector, 2.0f);
}
void reverseVector(float vector[]) {
multiplyVector(vector, -1.0f);
}
void threeQuarterVector(float vector[]) {
multiplyVector(vector, 3.0f/4.0f);
}
int main() {
//vector v
float v[3] = {4, -6, 7};
cout << "Vector v: ";
printVector(v);
//vector p
cout << "Vector p: ";
doubleVector(v);
//vector q
cout << "Vector q: ";
reverseVector(v);
//vector r
cout << "Vector r: ";
threeQuarterVector(v);
return 0;
}
=======Output========
Vector v: 4 -6 7
Vector p: 8 -12 14
Vector q: -4 6 -7
Vector r: 3 -4.5 5.25