Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2009
    Rep Power

    Default Finding a number in array close to another number

    I am having trouble coming up with a method that will take an array of doubles and find the number in the array closest to the average of the array. I know how to make a for loop to find the average but I don't know how to implement a method that will find the double in the array closest to the average. How (in pseudo code) would I implement this?

  2. #2
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Rep Power


    You could have two variables: double distance, and int index = 0. then loop through your array and compute the distance away from the average: (distance = Math.abs(average - array[i]) then the next time check if the distance of that number is less than the stored distance. If it is less, then store it's distance to the var distance, and its index to var index. If it is greater, go to next number in the array( continue;)
    Hope this helped.

  3. #3
    Join Date
    Mar 2009
    Rep Power

Similar Threads

  1. Finding the largest number in an array
    By starchildren3317 in forum New To Java
    Replies: 14
    Last Post: 11-03-2010, 06:49 AM
  2. Replies: 2
    Last Post: 12-10-2009, 12:06 PM
  3. finding the number of computer connected in LAN
    By sushil in forum Networking
    Replies: 1
    Last Post: 10-11-2009, 02:25 AM
  4. finding length on a number
    By thekrazykid in forum New To Java
    Replies: 8
    Last Post: 12-12-2008, 08:07 PM
  5. Finding the highest number
    By jigglywiggly in forum New To Java
    Replies: 7
    Last Post: 11-04-2008, 08:14 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts