Point determineClosestCentre(Point p, Point centreA, Point centreB) {

        //loop counter variable
        int l;

        //stores the distance between p and centreA
        double distanceA = 0.0;

        for (l = 0; l < p.dimension; l++) {
            //centroid coordinate of the point
            double centroidCoordinatePoint;
            if (p.weight != 0.0) {
                centroidCoordinatePoint = p.coordinates[l] / p.weight;
            } else {
                centroidCoordinatePoint = p.coordinates[l];
            }
            //centroid coordinate of the centre
            double centroidCoordinateCentre;
            if (centreA.weight != 0.0) {
                centroidCoordinateCentre = centreA.coordinates[l] / centreA.weight;
            } else {
                centroidCoordinateCentre = centreA.coordinates[l];
            }

            distanceA += (centroidCoordinatePoint - centroidCoordinateCentre) *
                    (centroidCoordinatePoint - centroidCoordinateCentre);
        }

        //stores the distance between p and centreB
        double distanceB = 0.0;

        for (l = 0; l < p.dimension; l++) {
            //centroid coordinate of the point
            double centroidCoordinatePoint;
            if (p.weight != 0.0) {
                centroidCoordinatePoint = p.coordinates[l] / p.weight;
            } else {
                centroidCoordinatePoint = p.coordinates[l];
            }
            //centroid coordinate of the centre
            double centroidCoordinateCentre;
            if (centreB.weight != 0.0) {
                centroidCoordinateCentre = centreB.coordinates[l] / centreB.weight;
            } else {
                centroidCoordinateCentre = centreB.coordinates[l];
            }

            distanceB += (centroidCoordinatePoint - centroidCoordinateCentre) *
                    (centroidCoordinatePoint - centroidCoordinateCentre);
        }

        //return the nearest centre
        if (distanceA < distanceB) {
            return centreA;
        } else {
            return centreB;
        }
    }