声明包含10个元素的Point对象数组,并计算距离最大两点之间的距离 。运行结果不正确,目测是循环语句有错,但是不知道怎么改,求指教
//仿照教材p198页面,声明包含10个元素的Point对象数组,并计算距离最大两点之间的距离。#include <iostream>
#include <cmath>
using namespace std;
class Point
{
public:
Point(float x=0,float y=0):x(x),y(y){}
float getX() const
{
return x;
}
float getY() const
{
return y;
}
private:
float x,y;
};
float distance(const Point points[],int nPoint)
{
int i,j,k;
int length[100];
float x,y;
float max=length[0];
for(k=0;k<100;k++)
{
for(i=0;i<nPoint;i++)
{
for(j=0;(j!=i)&&j<nPoint;j++)
{
x=points[i].getX()-points[j].getX();
y=points[i].getY()-points[j].getY();
length[k]=sqrt(x*x+y*y);
}
}
if(max<length[k])
max=length[k];
}
return max;
}
int main()
{
Point p[10]={Point(1,2),Point(2,3),Point(3,4),Point(4,5),Point(5,6),Point(6,7),Point(7,8),Point(8,9),Point(9,10),Point(10,11)};
float len=distance(p,10);
cout<<"the max distance is:"<<len<<endl;
return 0;
}