This is what I need my program to do:

1. Ask the user to enter the number of points.
2. Ask the user to enter x and y positions for each point. Store the positions in a 2-D array. What should the dimensions of the 2-D array be?
3. Compute the distance between the first two points and initialize the variable that represents the shortest distance. Recall that the distance between the two points (x1, y1) and (x2, y2) is computed by taking the square root of the quantity (x1 - x2)^2 + (y1 - y2)^2.
4. Use a nested for loop to compute the distance for every two points and update the shortest distance and the two points with the shortest distance.
5. Display the shortest distance and the closest two points.

This is where I have gotten so far, when I run it and calculate the distance I just get 0.0 every time.

//*****************************************************************************************************************************************
// Find Nearest Points
// 
// The GPS navigation system uses the graph and geometric algorithms to calculate distances and map a route. 
// One of the geometric problems is the closest-pair problem. Given a set of points, 
// the closest-pair problem is to find the two points that are nearest to each otherThe GPS navigation system uses 
// the graph and geometric algorithms to calculate distances and map a route. One of the geometric problems 
// is the closest-pair problem. Given a set of points, the closest-pair problem is to find the two points that are nearest to each other
//
//
//*****************************************************************************************************************************************
import java.util.Scanner;

public class Points
{
	public static void main(String[] agrs)
	{
		int x1, x2, y1, y2;
		int[][] table = new int[2][2];
		
		Scanner scan = new Scanner(System.in);
		
		for (x1=1; x1<table.length; x1++){
		System.out.println("Enter x1:");
		table[x1][0] = scan.nextInt();
		}
		for (x2=1; x2<table.length; x2++){
		System.out.println("Enter x2:");
		table[x2][0] = scan.nextInt();
		}
		for (y1=1; y1<table.length; y1++){
		System.out.println("Enter y1:");
		table[0][y1] = scan.nextInt();
		}
		for (y2=1; y2<table.length; y2++){
		System.out.println("Enter y2:");
		table[0][y2] = scan.nextInt();
		}
		
		double x = x1 - x2;
		double y = y1 - y2;
		double distance = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
		
		System.out.println(+ distance);
		
	}
}

You have detailed instructions. Why not follow them? Start at the beginning:
1. Ask the user to enter the number of points.
Then go on to step 2...
2. Ask the user to enter x and y positions for each point. Store the positions in a 2-D array. What should the dimensions of the 2-D array be?
etc.

ps: you get zero as the distance because x1, x2, y1 and y2 are all equal to 2, but don't worry about that because when you follow the instructions you won't use that code anyway.

Edited 4 Years Ago by JamesCherrill: n/a

Comments
undoing the downvote

I got this far

//*****************************************************************************************************************************************
// Find Nearest Points
// 
// The GPS navigation system uses the graph and geometric algorithms to calculate distances and map a route. 
// One of the geometric problems is the closest-pair problem. Given a set of points, 
// the closest-pair problem is to find the two points that are nearest to each otherThe GPS navigation system uses 
// the graph and geometric algorithms to calculate distances and map a route. One of the geometric problems 
// is the closest-pair problem. Given a set of points, the closest-pair problem is to find the two points that are nearest to each other
//
// Jeffrey Hoover
//*****************************************************************************************************************************************
import java.util.Scanner;

public class Points
{
	public static void main(String[] agrs)
	{
		double[][] table = new double[3][3];
		
		Scanner scan = new Scanner(System.in);
		
		for (int i=1; i<table.length; i++){
		System.out.println("Enter x" + i + ":");
		table[i][0] = scan.nextDouble();
		}
		for (int p=1; p<table.length; p++){
		System.out.println("Enter y" + p + ":");
		table[0][p] = scan.nextDouble();
		}
		
		double distance = Math.sqrt(Math.pow((table[1][0] - table[2][0]), 2) + 
			Math.pow((table[0][1] - table[0][2]), 2));
		
		System.out.println(+ distance);
	}
}

now I just need help figuring out

4. Use a nested for loop to compute the distance for every two points and update the shortest distance and the two points with the shortest distance.
5. Display the shortest distance and the closest two points.

You need to stop and listen and think. Throwing random code around won't get you anywhere. You have detailed instructions. Your instructions say:
1. Ask the user to enter the number of points.
When you have done that, go on to step 2.

Ok I got what your saying, but now I am still stuck at trying to enter x and y positions for each point and store the positions in a 2-D array

//*****************************************************************************************************************************************
// Find Nearest Points
// 
// The GPS navigation system uses the graph and geometric algorithms to calculate distances and map a route. 
// One of the geometric problems is the closest-pair problem. Given a set of points, 
// the closest-pair problem is to find the two points that are nearest to each otherThe GPS navigation system uses 
// the graph and geometric algorithms to calculate distances and map a route. One of the geometric problems 
// is the closest-pair problem. Given a set of points, the closest-pair problem is to find the two points that are nearest to each other
//
// Jeffrey Hoover
//*****************************************************************************************************************************************
import java.util.Scanner;

public class Points
{
	public static void main(String[] agrs)
	{
		int u=0;
		int p=2;
		double[][] table = new double[u][p];
		
		Scanner scan = new Scanner(System.in);
		
		System.out.println("Enter amount of points:");
		u = scan.nextInt();
		
		for (u=0; u<table.length; u++){
		System.out.println("Enter x" + u + ":");
		table[u][0] = scan.nextDouble();
		}
		for (p=0; p<table.length; p++){
		System.out.println("Enter y" + p + ":");
		table[0][p] = scan.nextDouble();
		}
		
		
	}
}

OK, good start.
You don't know what size the array needs to be until after the user has entered the number of points, so don't create it until you have a correct value for the variable you call u.

I an doing this same program and I cant figure out how to calculate the closest distance. Can someone please help

This article has been dead for over six months. Start a new discussion instead.