Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Distance formula with arrays, and loops. help?

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Location
    Montreal, Quebec, Canada
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Distance formula with arrays, and loops. help?

    Question 5: Calculate Distance (15 points)
    The final method you should write will involve a comparison of two arrays to calculate how similar they are to each other. Write a method called calculateDistance which takes as input two double[] and returns a double representing the Euclidean distance between the two arrays.
    The Euclidean distance can be computed by first calculating the sum of the square distances between the two arrays and then taking the square root of the entire thing (essentially the Pythagorean theorem). For example, if your arrays are acalled array1 and array2 and had size of 3 you could calculate the Euclidean distance by the following:
    sqrt(array1[0] − array2[0])2 + (array1[1] − array2[1])2 + (array1[2] − array2[2])
    Since you don’t know ahead of time how large the arrays will be, however, you need to use a loop. Your
    method should return this Euclidean distance.
    Your method may assume for simplicity that the size of the arrays will always be the same. That is, you do not need to add extra logic to handle that case.

    how do I start this? and how would I use a loop in this situation?
    all i know is this formula
    double distance = Math.sqrt(Math.pow((array1[x] - array2[x]),2))

    Thank you!!


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Distance formula with arrays, and loops. help?

    Quote Originally Posted by smith999 View Post
    ...
    ... how would I ...
    I don't get much out of the statement about Euclidean distance, so I'll try to express it in a way that makes sense to me.

    In words: The Euclidean distance between two numerical arrays of equal length is equal to the square root of the sum of the squares of the differences between corresponding array elements.

    It's actually easier to do than it is to talk about, and pseudo-code may be more clear than my words.

    Anyhow...

    See whether you think the following few lines of pseudo-code make sense according to the definition of Euclidean distance. If the pseudo-code makes sense, then implementation with Java is a snap. If it doesn't make sense to you, then you must think about it until it does!

    (Just kidding. If there are questions, it's always OK to ask. But think about it first.)

    This assumes that the two arrays are the same length, as your assignment stipulates.
     
    Declare a double precision variable, sumSquares.  Initialize sumSquares to 0.0;
     
    Repeat the following loop for integer values of k from 0 through the index of the last element of the arrays:
    BEGIN LOOP
        Add the square of (array1[k]-array2[k]) to sumSquares
    END LOOP
     
    Then take the square root of sumSquares.  That's the Euclidean distance between the two arrays
     
    Taa-Daa!



    Cheers!

    Z
    Last edited by Zaphod_b; October 22nd, 2012 at 06:41 PM.

Similar Threads

  1. i am having a problem with the distance formula,help needed!
    By Bentino in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 22nd, 2012, 07:07 PM
  2. [SOLVED] Putting Trajectory of a Projectile formula in Java form.
    By mwebb in forum Java Theory & Questions
    Replies: 2
    Last Post: March 2nd, 2012, 07:23 PM
  3. Method is not initiating formula
    By CrimsonFlash in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 22nd, 2011, 08:19 PM
  4. [SOLVED] Java assignment, concerning Arrays and Loops
    By trejorchest in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 17th, 2011, 12:59 PM
  5. Grades project/arrays, loops
    By rochla16 in forum Collections and Generics
    Replies: 1
    Last Post: March 29th, 2011, 12:26 AM