Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Vector2 is defined as: #include using namespace std; class Vector2 { private: double x, y; public: Vector2(double x, double y) : x(x), y(y) { }

Vector2 is defined as:

#include

using namespace std;

class Vector2 {

private:

double x, y;

public:

Vector2(double x, double y)

: x(x), y(y) { }

Vector2 operator+(const Vector2& v)

{

return Vector2(x + v.x, y + v.y);

}

Vector2 operator*(double s)

{

return Vector2(s * x, s * y);

}

double operator*(const Vector2& v) // dot product

{

return x * v.x + y * v.y;

}

void print() { // print vector

std::cout << x << ", " << y << ' ';

}

friend ostream& operator<<(ostream& out, const Vector2& v) {

out << v.x << ", " << v.y << ' ';

return out;

}

};

use C++ to complete the problem:

a) Create an array "allVectors" with 10 Vector2 objects (initialize them to random points between 0 and 1). b) Add a member function/method "NearestVector" to Vector2 for finding the nearest among a list of vectors:

Vector2 a(3,4); b = a.NearestVector(allVectors);

Note 1: (Mathematically speaking) assume the vectors start from origin, then each vector represents a point. Note 2: Use the Pythagorian the distance between two vectors (points).

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

Recommended Textbook for

OCA Oracle Database SQL Exam Guide Exam 1Z0-071

Authors: Steve O'Hearn

1st Edition

1259585492, 978-1259585494

More Books

Students also viewed these Databases questions

Question

To solve p + 3q = 5z + tan( y - 3x)

Answered: 1 week ago