Results 1 to 3 of 3
- 02-14-2010, 11:54 PM #1
Member
- Join Date
- Mar 2009
- Posts
- 31
- Rep Power
- 0
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?
- 02-15-2010, 12:03 AM #2
Senior Member
- Join Date
- Nov 2009
- Posts
- 235
- Rep Power
- 4
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.
- 02-15-2010, 12:37 AM #3
Member
- Join Date
- Mar 2009
- Posts
- 31
- Rep Power
- 0
Similar Threads
-
Finding the largest number in an array
By starchildren3317 in forum New To JavaReplies: 14Last Post: 11-03-2010, 06:49 AM -
Issue With Finding Total Number of Blank Spaces in File
By Cod in forum New To JavaReplies: 2Last Post: 12-10-2009, 12:06 PM -
finding the number of computer connected in LAN
By sushil in forum NetworkingReplies: 1Last Post: 10-11-2009, 02:25 AM -
finding length on a number
By thekrazykid in forum New To JavaReplies: 8Last Post: 12-12-2008, 08:07 PM -
Finding the highest number
By jigglywiggly in forum New To JavaReplies: 7Last Post: 11-04-2008, 08:14 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks