Please please help me to write .cpp files and main.cpp. The requirements for the
ID: 669456 • Letter: P
Question
Please please help me to write .cpp files and main.cpp. The requirements for the Point class are as follows:
Points should be able to have an arbitrary number of dimensions of doubles. That is, (1, 2), (1, 2, 4), (2, 3, ..., 5, 6), etc. You need to:
Add a private dim member.
Create at least one constructor which takes an int for the dimension and an array ofdoubles. Of course,
the array has to be at least as big as the dimension value. You will have to decide if it makes sense to
have a default constructor and what initialization it should do.
Take care of all consequences of this change, including updating the distanceTo()function.
Since the dimension could be potentially very large, you have to allocate it dynamically, and take care of
all cleanup. Memory leaks will be penalized.
dynamic allocation means: overload the "big three".
Point objects should be comparable. I need to:
Overload the comparison operators: ==, !=, <, >, <=, and >=. Hint: reuse the implementation of == for !=,
and so on. This makes the implementation more consistent, easy to ready, and less error prone.
Assume a lexicographic order, that is, (3, 4, 5) > (1, 2, 3) > (1, 2, 3) > (1, 1, 3) > (1, 1, 2), etc. In other
words, compare the first dimension, then the second, and so on.
Point arithmetic should be allowed. You need to:
Overload the arithmetic operators: +, . Addition and subtraction should be dimensionwise, that is, (1, 2, 3) + (4, 5, 6) gives (5, 7, 9). Similar with subtraction.
Create factorwise multiplication and division: *, /. That is, if p1 is a point (1, 2, 3), then p1*4 should give a point (4, 8, 12). Similar for division. (Remember to disallow division by zero.) Factors should be doubles.
Overload the compound assignment operators according to the rules in (1) and (2) above: +=, =, *=, and /=.
Cluster class
The requirements for the Cluster class are as follows:
The Cluster should be able to have an arbitrary number of Points. You have to:
Use a typedef statement to create an alias for the type Point * (that is, pointer to Point). Call it PointPtr.
Make sure it is global in your namespace.
Create a private nested structure type Node to support a singlylinked list. Hint: It only needs two
members!
Create a private singlylinked list member of the Cluster class to hold the Points. Hint: This is just a
pointer to the head.
Optional: You could, if you want, create an alias to the Node * type (that is, pointer to Node), and use
appropriately. If you do so, make sure it is visible where it is used.
Create a size member to keep track of how many Points you have in the Cluster.
A linked list is dynamically allocated by default: overload the big three.
Implement a default constructor, if it makes sense. Hint: A Cluster can remain empty and/or unused.
The linked list naturally supports an order of the elements. You have to:
1. Keep the Points contained in the Cluster in the lexicographic order mentioned in the requirements for the
Point class. Hint: Use the Point comparison operators when adding a Point.
3. Since Clusters contain PointPtrs, the corresponding points can be treated as a set, even if there are points
that are equal to each other. (Hint: Two points (1, 2, 3) and (1, 2, 3) will have different addresses.) You have to:
1. Overload operator+ to represent the UNION of two Clusters.
2. Overload operator to represent the INTERSECTION of two Clusters. Hint: This may be empty.
Overload operator==. It will return true iff (this means if and only if) two Clusters contain exactly the same points (that is, PointPtrs). Hint: The ordering might come useful here.
Overload the compound assignment operators accordingly.
4. Clusters should be able to to add and remove Points. You should:
Implement the add() and remove() functions to be able to add and remove PointPtr. Hint: See the signatures in the arraybased definition of the Cluster class
Overload operator+ to allow a statement like this: c1 = c1 + p1, where c1 is a Cluster and p1 is a Point. Hint: How similar or different is this to add()?
Overload operator to allow a statement like this: c2 = c2 p2, where c2 is a Cluster and p2 is a Point. Hint: How similar or different is this to remove()?
Overload the compound operators accordingly.
/// Point /////
#ifndef CLUSTERING_POINT_H
#define CLUSTERING_POINT_H
#include <iostream>
namespace Clustering {
class Point {
int dim; // number of dimensions of the point
double *values; // values of the point's dimensions
public:
Point(int);
Point(int, double *);
// Big three: cpy ctor, overloaded operator=, dtor
Point(const Point &);
Point &operator=(const Point &);
~Point();
// Accessors & mutators
int getDims() const { return dim; }
void setValue(int, double);
double getValue(int) const;
// Functions
double distanceTo(const Point &) const;
// Overloaded operators
// Members
Point &operator*=(double);
Point &operator/=(double);
const Point operator*(double) const; // prevent (p1*2) = p2;
const Point operator/(double) const;
double &operator[](int index) { return values[index - 1]; } // TODO out-of-bds?
// Friends
friend Point &operator+=(Point &, const Point &);
friend Point &operator-=(Point &, const Point &);
friend const Point operator+(const Point &, const Point &);
friend const Point operator-(const Point &, const Point &);
friend bool operator==(const Point &, const Point &);
friend bool operator!=(const Point &, const Point &);
friend bool operator<(const Point &, const Point &);
friend bool operator>(const Point &, const Point &);
friend bool operator<=(const Point &, const Point &);
friend bool operator>=(const Point &, const Point &);
friend std::ostream &operator<<(std::ostream &, const Point &);
friend std::istream &operator>>(std::istream &, Point &);
};
}
#endif //CLUSTERING_POINT_H
/// Cluster/////
Explanation / Answer
#include <bits/stdc++.h>
using namespace std;
class Point{
public:
double[] co_ord;
int dim;
Point(int dim,double[] arr){
co_ord = new[dim];
for(int i = 0; i < dim; i++)
co_ord[i] = arr[i];
}
double distTo(Point* nPoint){
int dim = min(nPoint->dim,dim);
double dist = 0.0;
for (int i = 0; i < dim; i++)
dist += pow(nPoint->co_ord[i] - co_ord[i],2);
int rem = abs(nPoint->dim-dim);
if (nPoint->dim > dim){
for (int i = dim; i < dim+rem; i++)
dist += pow(nPoint->co_ord[dim],2);
}
else{
for (int i = dim; i < dim+rem; i++)
dist += pow(co_ord[dim],2);
}
return dis;
}
Point* addition(Point* nPoint){
Point* p;
int dim = max(nPoint->dim,dim);
double[] temp_arr = new double[dim];
for (int i = 0; i < dim; i++){
if (i < nPoint->dim)
temp_arr[i] += nPoint->co_ord[i];
if (i < dim);
temp_arr[i] += co_ord[i];
}
p = new Point(dim,temp_arr);
return p;
}
Point* subtraction(Point* nPoint){
Point* p;
int dim = max(nPoint->dim,dim);
double[] temp_arr = new double[dim];
for (int i = 0; i < dim; i++){
if (i < nPoint->dim)
temp_arr[i] -= nPoint->co_ord[i];
if (i < dim);
temp_arr[i] += co_ord[i];
}
p = new Point(dim,temp_arr);
return p;
}
Point* multiplication(int fac){
Point* p;
double[] temp_arr = new double[dim];
for (int i = 0; i < dim; i++){
temp_arr[i] = co_ord[i]*fac;
}
p = new Point(dim,temp_arr);
return p;
}
Point* division(int fac){
Point* p;
double[] temp_arr = new double[dim];
for (int i = 0; i < dim; i++){
temp_arr[i] = co_ord[i]/fac;
}
p = new Point(dim,temp_arr);
return p;
}
bool operator==(Point* nPoint){
if (dim == nPoint->dim){
for (int i = 0; i < dim; i++){
if (co_ord[i] != nPoint->co_ord[i])
return false;
}
return true;
}
return false;
}
bool operator!=(Point* nPoint){
if (dim == nPoint->dim){
for (int i = 0; i < dim; i++){
if (co_ord[i] == nPoint->co_ord[i])
return false;
}
return true;
}
return true;
}
bool operator<(Point* nPoint){
if (dim == nPoint->dim){
for (int i = 0; i < dim; i++){
if (co_ord[i] >= nPoint->co_ord[i])
return false;
}
return true;
}
return false;
}
bool operator<(Point* nPoint){
if (dim == nPoint->dim){
for (int i = 0; i < dim; i++){
if (co_ord[i] <= nPoint->co_ord[i])
return false;
}
return true;
}
return false;
}
bool operator<=(Point* nPoint){
if (dim == nPoint->dim){
for (int i = 0; i < dim; i++){
if (co_ord[i] > nPoint->co_ord[i])
return false;
}
return true;
}
return false;
}
bool operator<(Point* nPoint){
if (dim == nPoint->dim){
for (int i = 0; i < dim; i++){
if (co_ord[i] < nPoint->co_ord[i])
return false;
}
return true;
}
return false;
}
};
class cluster{
public:
int nop;
vector<Point*> my_vec;
cluster(){
nop = 0;
}
cluster* operator+(Point* p){
nop++;
my_vec.push_back(p);
cluster *c = new cluster();
c->nop = nop;
c->my_vec = my_vec;
return c;
}
void operator-(Point* p){
nop--;
my_vec.pop_back(p);
cluster *c = new cluster();
c->nop = nop;
c->my_vec = my_vec;
return c;
}
bool operator==(cluster* c){
if (c->nop == nop){
for (int i = 0; i < nop; i++){
if (my_vec[i] != c->my_vec[i])
return false;
}
return true;
}
return false;
}
bool operator!=(cluster* c){
if (c->nop == nop){
for (int i = 0; i < nop; i++){
if (my_vec[i] == c->my_vec[i])
return false;
}
return true;
}
return true;
}
};
int main(){
return 0;
}