In: Computer Science
17.11 Worksheet 9A
Point |
---|
- x:double - y:double |
+Point() +Point(x:double, y:double) +setPoint(x: double y:double):void +setX(x:double):void +setY(y:double):void +getX():double +getY():double +findDistance(Point point):double +print():void |
The point is (3.5, 5.4).
Below is main.cpp - follow the instructions in the comments
#include <iostream> #include <string> #include <cmath> #include <fstream> #include "Point.h" using namespace std; void loadArray(Point points[], int& numE, string filename); int main() { Point point1 = Point(); Point point2 = Point(1, 0); Point points[50]; string filename = "points1.txt"; int numE = 0; double totalPoint1 = 0; double totalPoint2 = 0; loadArray(points, numE, filename); totalPoint1 = findTotalDistance(points, point1, numE); totalPoint2 = findTotalDistance(points, point2, numE); cout.setf(ios::fixed); cout.precision(1); cout << "The total distance from point 1 to the points in the array is " << totalPoint1 << "." << endl; cout << "The total distance from point 2 to the points in the array is " << totalPoint2 << "." << endl; } void loadArray(Point points[], int& numE, string filename) { ifstream fin; double point1; double point2; numE = 0; fin.open(filename); if(fin.fail()) { cerr << "File failed to open" << endl; exit(1); } fin >> point1; while(!fin.eof() && numE < 50) { fin >> point2; points[numE].setPoint(point1, point2); numE++; fin >> point1; } } // Write a method called find total distance that finds the distance from a point to every other point in the array. // It should return the total of those distances.
Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
_________________
// points1.txt
10 2
3 -4
5 6
7 8
9 -3
4 6
6 8
______________________
// Point.h
#ifndef POINT_H
#define POINT_H
class Point
{
public:
Point();
Point(double x,double y);
void setPoint(double x,double y);
void setX(double x);
void setY(double y);
double getX();
double getY();
private:
// Declaring variables
double x;
double y;
};
#endif
_________________________
// Point.cpp
#include <iostream>
using namespace std;
#include "Point.h"
Point::Point()
{
this->x=0;
this->y=0;
}
Point::Point(double x,double y)
{
this->x=x;
this->y=y;
}
void Point::setPoint(double x,double y)
{
this->x=x;
this->y=y;
}
void Point::setX(double x)
{
this->x=x;
}
void Point::setY(double y)
{
this->y=y;
}
double Point::getX()
{
return x;
}
double Point::getY()
{
return y;
}
__________________________
// main.cpp
#include <iostream>
#include <string>
#include <cmath>
#include <cstdlib>
#include <fstream>
#include "Point.h"
using namespace std;
void loadArray(Point points[], int& numE, string
filename);
double findTotalDistance(Point points[],Point point1,int
numE);
int main() {
Point point1 = Point();
Point point2 = Point(1, 0);
Point points[50];
string filename = "points1.txt";
int numE = 0;
double totalPoint1 = 0;
double totalPoint2 = 0;
loadArray(points, numE, filename);
totalPoint1 = findTotalDistance(points, point1,
numE);
totalPoint2 = findTotalDistance(points, point2,
numE);
cout.setf(ios::fixed);
cout.precision(1);
cout << "The total distance from point 1 to the points in the
array is " << totalPoint1 << "." << endl;
cout << "The total distance from point 2 to the points in the
array is " << totalPoint2 << "." << endl;
}
void loadArray(Point points[], int& numE, string
filename) {
ifstream fin;
double point1;
double point2;
numE = 0;
fin.open(filename.c_str());
if(fin.fail()) {
cerr << "File failed to open" << endl;
exit(0);
}
fin >> point1;
while(!fin.eof() && numE < 50) {
fin >> point2;
points[numE].setPoint(point1, point2);
numE++;
fin >> point1;
}
}
// Write a method called find total distance that
finds the distance from a point to every other point in the
array.
// It should return the total of those distances.
double findTotalDistance(Point points[],Point point1,int
numE)
{
double totDis=0;
for(int i=0;i<numE;i++)
{
totDis+=sqrt(pow((points[i].getX() - point1.getX()), 2) +
pow((points[i].getY() - point1.getY()), 2));
}
return totDis;
}
______________________________
Output:
_______________Could you plz rate me well.Thank
You