私はウェブサイトとプログラミング全般に不慣れなので、我慢してください。
私のプログラムは次の基準を満たしている必要があります。
正の整数
n
に続いてポイントの(x,y
)座標を読み取り、これらの値を、行と2つの列n
を持つpointsという名前の2次元配列に格納します。n
distances [i] [j] = points[i]からpoints[j]までの距離で、distancesという名前の
n
行と列を持つ2次元配列を作成します。n
averages [i] =距離
n
の行の平均で、averagesという名前の長さの1次元配列を作成します。i
n
どのポイントの平均距離が最小であるかを判別して印刷します。
パート#3で困惑しているように見えるパートですが、行の平均距離をどのように計算しますか?
これまでの私のコードは次のとおりです。誰かが私がそれを完了するのを手伝ってくれるなら、私は大いに感謝します。また、私が持っているものの間違いを指摘してください。
import java.util.Scanner;
public class Java
{
public static void main( String[] args )
{
Scanner input = new Scanner(System.in );
System.out.print("How many points: ");
int n = input.nextInt();
double[][] points = new double[n][2];
for(int i = 0; i < n; i++)
{
//prompt or and get coordinates
//points[i][0] = input.nextDouble();
//points[i][1] = input.nextDouble();
}
double[][] distances = new double[n][n];
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
distances[i][j] = distance(points[i], points[j]);
double[] averages = new double[n];
for(int i = 0; i < n; i++)
averages[i] = average( distances[i] );
int which_one = minimum_location( averages );
System.out.printf("Point # %d has a smallest average of %f\n", which_one,
averages[which_one]);
}
public static double distance
(double x1, double y1, double x2, double y2)
{
return Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
}
{
public static double average( double[] x);
//problem area
}
{
public static int minimum_location( double[] x );
if (shortestDistance > distance);
{
p1 = i;
p2 = j;
shortestDistance = distance;
}
}