about | help | code help+videos | done | prefs |
A "magic square" is an n by n grid of positive integers such that every row, column, and diagonal adds up to the same number. That number is said to be the sum of the magic square. Magic squares use all the integers from 1 up to the largest number that can fit in the grid, counting up by 1 each time. For example, a 3 by 3 magic square will use all of the integers from 1 to 9. A 4 by 4 magic square will use all of the integers from 1 to 16. Write a method that returns the sum of the given magic square or returns -1 if the square is not a magic square. For this method, you may assume that the correct integers are being used and therefore only need to check to make sure the sums all come out the same. Input parameters: an integer indicating the dimensions of the grid; a one dimensional array of integers representing the two dimensional grid (when you get to the end of a row, start a new one). Return value: Returns an integer that is either the sum of the magic square, or -1 if the square is not magic. NOTE: It is not possible to have a 2 by 2 magic square. magicSquareSLAPHL_Dec2011(1, [1]) → 1 magicSquareSLAPHL_Dec2011(2, [1, 3, 2, 4]) → -1 magicSquareSLAPHL_Dec2011(2, [3, 1, 4, 2]) → -1 ...Save, Compile, Run (ctrl-enter) |
Progress graphs:
Your progress graph for this problem
Random user progress graph for this problem
Random Epic Progress Graph
Difficulty: 250
Copyright Nick Parlante 2017 - privacy