brewersfan1976

border_sums.rb

May 20th, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.34 KB | None | 0 0
  1. def borderSums(matrix)
  2.     sum = 0
  3.     array = Array.new
  4.     array_length = matrix.length
  5.     length = matrix.length
  6.     x = 0
  7.     y = 0
  8.     border = 0
  9.     count = 0
  10.     number_of_borders = array_length / 2
  11.  
  12.     while (border < number_of_borders)
  13.         x = border
  14.         y = border
  15.      
  16.         while (count < length)
  17.               sum = sum + matrix[x][y]
  18.               y = y + 1
  19.               count = count + 1
  20.         end
  21.    
  22.         x = border + 1
  23.         y = border
  24.         count = 1
  25.        
  26.         while (count < length)
  27.               sum = sum + matrix[x][y]
  28.               x = x + 1
  29.               count = count + 1
  30.         end
  31.        
  32.         x = array_length - 1 - border
  33.         y = border + 1
  34.         count = 1
  35.      
  36.         while (count < length)
  37.               sum = sum + matrix[x][y]
  38.               y = y + 1
  39.               count = count + 1
  40.         end
  41.        
  42.         x = border + 1
  43.         y = array_length - 1 - border
  44.         count = 2
  45.      
  46.         while (count < length)
  47.               sum = sum + matrix[x][y]
  48.               x = x + 1
  49.               count = count + 1
  50.         end
  51.      
  52.         x = 0
  53.         y = 0
  54.        
  55.         array[border] = sum
  56.         sum = 0
  57.         count = 0
  58.         border = border + 1
  59.         length = length - 2
  60.     end    
  61.      
  62.     return array
  63. end
Advertisement
Add Comment
Please, Sign In to add comment