Need help figuring out what the read_vector and write_vector functions are askin
ID: 646053 • Letter: N
Question
Need help figuring out what the read_vector and write_vector functions are asking for. Here is the prompt:
A 2D vector (u, v) has its base at the origin in the cartesian coordinate system, i.e.
the x-y axes, and its tip (or arrow) at x = u and y = v. Thus, a 2D vector can be
represented simply as an (x; y) point in the cartesian coordinate system. Write a program
that reads in a pair of 2D vectors and a scalar value and then applies the following vector
operations: addition, subtraction, scalar multiplication, and perpendicularity. The main
routine of the program is already provided in vector2D_template.cpp (which you copied
into vector2D.cpp). Your task is to add eight functions read_vector(), vector_length(),
write_vector(), vector_add(), vector_subtract(), scalar_mult(), normalize(), and
perpendicular() so that the program produces the desired results.
This is the template
/*
File: vector2D.cpp
Created by: ??
Creation Date: ??
Synopsis: ??
*/
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
const double EPSILON(1e-12);
// function prototypes
// ENTER FUNCTION PROTOTYPE FOR read_vector HERE.
// ENTER FUNCTION PROTOTYPE FOR vector_length HERE.
// ENTER FUNCTION PROTOTYPE FOR write_vector HERE.
// ENTER FUNCTION PROTOTYPE FOR vector_add HERE.
// ENTER FUNCTION PROTOTYPE FOR vector_subtract HERE.
// ENTER FUNCTION PROTOTYPE FOR scalar_mult HERE.
// ENTER FUNCTION PROTOTYPE FOR normalize HERE.
// ENTER FUNCTION PROTOTYPE FOR perpendicular HERE.
// *** DO NOT CHANGE ANY CODE IN THE MAIN FUNCTION.
int main()
{
double u1, v1; // coordinates of first vector
double u2, v2; // coordinates of second vector
double u3, v3;
double scalar;
read_vector("Enter first vector (2 floats): ", u1, v1);
read_vector("Enter second vector (2 floats): ", u2, v2);
cout << "Enter scalar multiplier: ";
cin >> scalar;
cout << endl;
write_vector("First vector: ", u1, v1);
write_vector("Second vector: ", u2, v2);
cout << endl;
vector_add(u1, v1, u2, v2, u3, v3);
write_vector("Vector add: ", u3, v3);
vector_subtract(u1, v1, u2, v2, u3, v3);
write_vector("Vector subtract: ", u3, v3);
scalar_mult(u1, v1, scalar, u3, v3);
write_vector("Scalar multiplier: ", u3, v3);
cout << endl;
write_vector("First vector: ", u1, v1);
write_vector("Second vector: ", u2, v2);
perpendicular(u1, v1, u2, v2);
return(0);
}
// DEFINE FUNCTION read_vector HERE.
// DEFINE FUNCTION vector_length HERE.
// DEFINE FUNCTION write_vector HERE.
// DEFINE FUNCTION vector_add HERE.
// DEFINE FUNCTION vector_subtract HERE.
// DEFINE FUNCTION scalar_mult HERE.
// DEFINE FUNCTION normalize HERE.
// DEFINE FUNCTION perpendicular HERE.
Explanation / Answer
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
const double EPSILON(1e-12);
// function with prototypes
void read_vector (const string & prompt, double & x, double & y); // ENTER THE FUNCTION OF PROTOTYPE FOR read_vector HERE.
double vector_length (double x, double y); // ENTER THE FUNCTION OF PROTOTYPE FOR vector_length HERE.
void write_vector (const string & prompt, double & x, double & y); // ENTER THE FUNCTION OF PROTOTYPE FOR write_vector HERE.
void vector_add (double x1, double y1, double x2, double y2, double & x3, double & y3); // ENTER THE FUNCTION OF PROTOTYPE FOR vector_add HERE.
void vector_subtract (double x1, double y1, double x2, double y2, double & x3, double & y3); // ENTER THE FUNCTION OF PROTOTYPE FOR vector_subtract HERE.
void scalar_mult (double x1, double y1, double s, double & x2, double & y2); // ENTER THE FUNCTION OF PROTOTYPE FOR scalar_mult HERE.
void normalize (double & x, double & y); // ENTER THE FUNCTION OF PROTOTYPE FOR normalize HERE.
void perpendicular (double & x1, double & y1, double & x2, double & y2); // ENTER THE FUNCTION OF PROTOTYPE FOR perpendicular HERE.
// *** DO NOT CHANGE ANY CODE IN THE MAIN FUNCTION.
int main()
{
double u1, v1; // coordinates of first vector
double u2, v2; // coordinates of second vector double u3, v3;
double scalar;
read_vector("Enter first vector (2 floats): ", u1, v1);
read_vector("Enter second vector (2 floats): ", u2, v2);
cout > scalar; cout << endl;
write_vector("First vector: ", u1, v1);
write_vector("Second vector: ", u2, v2);
cout << endl; vector_add(u1, v1, u2, v2, u3, v3);
write_vector("Vector add: ", u3, v3);
vector_subtract(u1, v1, u2, v2, u3, v3);
write_vector("Vector subtract: ", u3, v3);
scalar_mult(u1, v1, scalar, u3, v3);
write_vector("Scalar multiplier: ", u3, v3);
cout << endl; write_vector("First vector: ", u1, v1);
write_vector("Second vector: ", u2, v2);
perpendicular(u1, v1, u2, v2);
return(0);
}
void read_vector (const string & prompt, double & x, double & y) // DEFINE THE FUNCTION read_vector HERE.
{
cout << prompt;
cin >> x >> y;
}
double vector_length (double x, double y) // DEFINE THE FUNCTION vector_length HERE.
{
double v_length = sqrt(pow(x, 2) + pow(y, 2));
return (v_length);
}
void write_vector (const string & prompt, double & x, double & y) // DEFINE THE FUNCTION write_vector HERE.
{
cout << "(" << x << ", " << y << ") has length " << vector_length(x,y) << endl;
}
void vector_add (double x1, double y1, double x2, double y2, double & x3, double & y3) // DEFINE THE FUNCTION vector_add HERE.
{
x3 = x1 + x2;
y3 = y1 + y2;
cout << "The Vector add: ";
}
void vector_subtract (double x1, double y1, double x2, double y2, double & x3, double & y3) // DEFINE THE FUNCTION vector_subtract HERE.
{
x3 = x1 - x2;
y3 = y1 - y2;
cout << "TheVector subtract: ";
}
void scalar_mult (double x1, double y1, double s, double & x2, double & y2) // DEFINE THE FUNCTION scalar_mult HERE.
{
x2 = s * x1;
y2 = s * y1;
cout << "The Vector multiplier: ";
}
void normalize (double & x, double & y) // DEFINE THE FUNCTION normalize HERE.
{
double v = vector_length(x, y);
if (v < EPSILON)
{
x = x / v;
y = y / v;
}
}
void perpendicular (double & x1, double & y1, double & x2, double & y2) // DEFINE THE FUNCTION perpendicular HERE.
{
normalize(x1, y1);
normalize (x2, y2);
double p1x = -y1;
double p1y = x1;
double p2x = -p1x;
double p2y = -p1y;
if (abs(x2 - p1x) < EPSILON && abs(y2 - p1y) < EPSILON || (abs(x2 - p2x) < EPSILON && abs(y2 - p2y) < EPSILON))
{
cout << "The Vectors are PERPENDICULAR." << endl;
}
else
{
cout << "The Vectors are NOT PERPENDICULAR." << endl;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.