brewersfan1976

array_center.rb

Sep 6th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.51 KB | None | 0 0
  1. def arrayCenter(a)
  2.     output_array = Array.new
  3.     array_length = a.length
  4.     x = 0
  5.     y = 0
  6.     minA = a.min
  7.     sum = 0
  8.    
  9.     while (x < array_length)
  10.           sum = sum + a[x]
  11.           x = x + 1
  12.     end
  13.    
  14.     avgA = sum / array_length.to_f
  15.    
  16.     x = 0
  17.    
  18.     while (x < array_length)
  19.           if (a[x] - avgA).abs < minA then
  20.              output_array[y] = a[x]
  21.              y = y + 1
  22.           end
  23.              
  24.           x = x + 1
  25.     end
  26.        
  27.     return output_array
  28. end
Advertisement
Add Comment
Please, Sign In to add comment