In: Computer Science
in C++
Implement all functions in line (inside the class header file)
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).
#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.