开发者

Arrays. Histograms, dividing array into subset and counting number of elements in each group. Java

Hi im really confused abt this method which i have to create. I have to create a histogram array with num division categories i.e selecting a reasonable integer range and step size for numdivisons to cover whole range of elements in an array. @return array histogram in which position i contains the * number of observations in division i */

example: {1,3,4,5,10,15,17}.

If numdivisions=2 then we 开发者_StackOverflowneed to divide the range (1 to 17) into 2 divisions. For example, range 0 to 10 (not included) and 10 to 20. In this case there are 4 values in the range 0 to <10 and 3 values between 10 and 20.

This histogram for numdivisions=2 would be the array {4,3}.

public int[] histogram(int numdivisions) can anyone tell me what would be the best method of doing it? thanks


Simple:

  1. Get the minimum and maximum elements of your array
  2. Take the difference and divide it by num, obtaining "step"
  3. Now iterate through the array, placing the number n in the slot i such that i * step <= n - min < (i+1) * step
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜