Question

In: Computer Science

in C++ Write a definition for a class ‘point’ to describe a point with coordinates (X,Y)...

in C++

  1. Write a definition for a class ‘point’ to describe a point with coordinates (X,Y) in 2-D space. The private data members are the coordinates X and Y of type ‘double’. The public member functions are:
  • A default constructor (no parameters) to initialize X and Y to zero.
  • An explicit value constructor with two parameters to initialize the values of X and Y.
  • Two functions, one to set the X value and the other to set the Y value of a point.
  • Two functions, one to return the X value and the other to return the Y value of a point.
  • A function to return the distance from the point (X,Y) to another given point.

Implement all functions in line (inside the class header file)

  1. Implement a user application function Maxpoint that uses the point class:

The function receives an array of points A of length N and returns the index of the point having the maximum distance from the origin (0.0, 0.0).

Solutions

Expert Solution

#include <iostream>
#include <string>
#include <cmath>
using namespace std;
class point{
  public:
  double x;
  double y;
  point(){
      x=0;
      y=0;
  }
  point(int x1,int y1){
      x=x1;
      y=y1;
  }
  void setX(int x1){
      x=x1;
  }
  void setY(int y1){
      y=y1;
  }
  double getX(){
      return x;
  }
  double getY(){
      return y;
  }
  double distance(){
      return sqrt(x*x+y*y);
  }
   
};
int Maxpoint(int arr[][2]){
    int len=4;
    double d[len];
    for(int i=0;i<len;i++){
        point p(arr[i][0],arr[i][1]);
        d[i]=p.distance();
    }
    double max=d[0];
    int index;
    for(int i=0;i<len;i++){
        if(max<d[i]){
            max=d[i];
            index=i;
        }
    }
    return index;
}
int main()
{ 
    int a[4][2];
    a[0][0]=5;
    a[0][1]=6;
    a[1][0]=8;
    a[1][1]=7;
     a[2][0]=9;
    a[2][1]=8;
     a[3][0]=3;
    a[3][1]=4;
    cout<<"The index of the point having the maximum distance from the origin (0.0, 0.0) is: "<<Maxpoint(a);
    return 0;
}

Thank you! if you have any queries post it below in the comment section I will try my best to resolve your queries and I will add it to my answer if required. Please give upvote if you like it.


Related Solutions

Given the definition for a Point class that holds the coordinates of the point as double...
Given the definition for a Point class that holds the coordinates of the point as double values x and y, write a function called pt_dist that takes two points and returns the straight-line distance between them (as a double). Use two ways, pt_dist function version and the pt_dist method version. In main, include two if else tests for each, If passed "TEST PASSED, DIST IS " else "Test Failed, dist is ". Hint: Rhymes with Bythagorean Beorem. #include <iostream> #include...
Design a class named GeoPoint to represent a point with x- and y-coordinates. The class contains:  ...
Design a class named GeoPoint to represent a point with x- and y-coordinates. The class contains:   The data fields x and y that represent the coordinates with gette and setter methods. A no-arg constructor that creates a point (0, 0).   A constructor that constructs a point with specified coordinates. The method equals(GeoPoint p) that returns true if two GeoPoint objects have the same x- and y-coordinates. Write a test program that creates an array of GeoPoint objects. The size of...
Design a class named MyPoint to represent a point with x- and y-coordinates. The class should...
Design a class named MyPoint to represent a point with x- and y-coordinates. The class should contain: Two data fields x and y that represent the coordinates. A no-arg constructor that creates a point at (0, 0). A constructor that creates a point with specified coordinates. Get methods for data fields x and y respectively. A method named distance that returns the distance from this point to another point with specified x- and y-coordinates. Use the formula: root (x2-x1)2 +...
A Point Class Definition A two-dimensional point may be represented by an x- and y-coordinate. In...
A Point Class Definition A two-dimensional point may be represented by an x- and y-coordinate. In this problem, you will write a Point class which stores this information and provides useful methods to work with points. We have provided an __init__ definition for the Point class. You will note that this accepts two arguments, being the x- and y-coordinates of the point. We have also included a __repr__ method, which will provide a canonical string representation of the point. Make...
C Language Let us define a Point type to store two-dimensional coordinates (x, y)! Write the...
C Language Let us define a Point type to store two-dimensional coordinates (x, y)! Write the following functions operating on this data type: dist(): calculates the distance between the two points received (using the Pythagorean theorem) equal(): checks if to points are equal or not read(): reads a point from the keyboard and returns it In the main, define two points, and test all these functions. When all tests are passed, solve the following task by utilizing the structure and...
Find the coordinates of the point (x, y, z) on the plane z = 4 x...
Find the coordinates of the point (x, y, z) on the plane z = 4 x + 1 y + 4 which is closest to the origin.
The coordinates of a point P in the x-y plane are (2;3) meters. q1 = 10...
The coordinates of a point P in the x-y plane are (2;3) meters. q1 = 10 C load (1;0) , q2 = 20 C load (-1;0) is placed in the metre position. The electric flied formed by these charges at the P point, find your size and direction.
(a). The Cartesian coordinates (x, y) = (−4, 4) of a point are given. Find polar...
(a). The Cartesian coordinates (x, y) = (−4, 4) of a point are given. Find polar coordinates (r, θ) of the point so that r > 0 and 0 ≤ θ ≤ 2π. (b). The polar coordinates (r, θ) = (−3, 5π/6) of a point are given. Find the Cartesian coordinates (x, y) of this point.
The following charges exist (given coordinates are (x, y) coordinates in the plane of the page):
  The following charges exist (given coordinates are (x, y) coordinates in the plane of the page): • a −3.0 µC point charge located at (0, 0) • a −2.0 µC uniform spherical shell of charge of radius 3.0 cm centered at (+4.0 cm, 0) • a +2.0 µC uniform spherical shell of charge of radius 2.0 cm centered at (0, −4.0 cm) • a +4.0 µC uniform spherical shell of charge of radius 4.0 cm centered at (−1.0 cm,...
Write a C++ program that design a class definition to be put in a header file...
Write a C++ program that design a class definition to be put in a header file called fizzjazz.h A store sells online FizzJazz which are sound tones made by famous bands. For each FizzJazz, the store wants to keep track of the following attributes: * title - the name of the sound tone * band - Famous band name that created the tone * duration - this is in seconds and may be fractional: examples: 20.0, 34.5 Each attribute will...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT