4

I've been assigned a school assignment to make and check for a 'Magic Square' in a 2d array generated by the user in a N*N matrix.

So far, I have gotten most of the code right (I have tested each method individually). However, I am unable to correct two final errors that keep popping up within my 'sumColumn' and 'sumRow' methods. Here is my code for the two aforementioned methods:

public static int sumColumn(int[][] square, int columnNumber)
{
    int sum = 0 ;
    for (int j = 0; j < square.length ; j++)
    {
        for (int i = 0; i < square.length; i++)
        {
            sum = sum + square[i][j] ;
        }
    }                        
    return sum ;
}

public static int sumRow(int[][] square, int rowNumber)
{
    int sum = 0 ;
    for (int i = 0; i < square.length; i++)
    {
        for (int j = 0; j < square.length; j++)
        {
            sum = sum + square[i][j] ;
        }
    }
    return sum ;
}

And here is the output along with the error that appears upon calling from the main method:

Please enter a value for N:
1
Please enter 1 numbers: 
1
This is the square you input:
+-+
|1|
+-+
Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
The method sumRow(int[][], int) in the type MagicSquares is not applicable for the arguments (int[][]) at squares.MagicSquares.validMagicSquare(MagicSquares.java:105)
The method sumColumn(int[][], int) in the type MagicSquares is not applicable for the arguments (int[][]) at squares.MagicSquares.main(MagicSquares.java:167)

Some fiddling with 'sumRow' and 'sumColumn' will instead yield another error:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
rowNumber cannot be resolved to a variable at squares.MagicSquares.validMagicSquare(MagicSquares.java:105)
colNumber cannot be resolved to a variable at squares.MagicSquares.main(MagicSquares.java:167)

Any help would be much appreciated in resolving this issue! Thanks!

PS: I've only started programming last month, so be kind :3

Edit: here is the method which checks if each row, column and main and secondary diagonals are equal to make a Magic Square.

boolean status = true ;
    int sum = sumDiagonal(square) ;
    if (sumSecondaryDiagonal(square) != sum)
    {
        status = false ;
    }
    else
    {
        for (int row = 0; (row < square.length) && status; row ++)
        {
            if (sum != sumRow(square, square.length))
            {
                status = false ;
            }
        }
        for (int col = 0; (col < square.length) && status; col ++)
        {
            if (sum != sumColumn(square, square.length))
            {
                status = false ;
            }
        } 
    }
    return status;

2 Answers2

0

Your sumColumn method expects two arguments:

  1. int[][] square
  2. int columnNumber

In your main method, it seems you are just providing a single argument, the int[][], and you forgot to include the columnNumber as a second argument.

Same applies to the sumRow method.

I made the above findings based on the error message you posted:

The method sumRow(int[][], int) in the type MagicSquares is not applicable for the arguments (int[][]) at squares.MagicSquares.validMagicSquare(MagicSquares.java:105)

It says that, in your MagicSquares.java file, line number 105, you invoke sumRow method. It further indicates that (The method sumRow(int[][], int) in the type MagicSquares), the method you implemented, can not be used/is not applicable for a call with arguments (int[][]), at line 105.

Amr
  • 2,420
  • 2
  • 17
  • 26
  • Thanks for the quick reply! Yes, I am unsure on how to include 'int rowNumber' and 'int columnNumber' into their respective methods. As per my assignments rules, I cannot get rid of these variables, nor can I alter the main method in any way ): –  Aug 31 '15 at 10:09
  • I am not suggesting for you to get rid of them. Instead, in your main method, you should include this argument, example: int x = sumRow(square, 5) – Amr Aug 31 '15 at 10:13
  • You're welcome. Now to make everything complete, mark an answer as "Accepted" :) – Amr Aug 31 '15 at 11:01
0

Your shown code does not have syntax errors.

But your exception:

The method sumRow(int[][], int) in the type MagicSquares is not applicable for the arguments (int[][]) at squares.MagicSquares.validMagicSquare(MagicSquares.java:105)

You don't make the right call to sumColumn and sumRow when calling them:

int[][] matrix = // your matrix;
sumRow(matrix);     // bad call
sumRow(matrix, 1);  // good call!!!!

But actually, if you check the methods you dont use columnNumber and rowNumber so if you want to sum all columns / rows redeclare your methods as now are written like this:

public static int sumAllColumns(int[][] square)
public static int sumAllRows(int[][] square)

And make sure 2nd loop iterates over columns!!!

for (int j = 0; j < square[0].length; j++)

I MUST not change the heading of any methods.

So, just use methods already existing, but make sure you use the columnNumber and rowNumber variables. If method description is the right one, you only need to sum given row / column, so no nested loops needed.

public static int sumColumn(int[][] square, int columnNumber) {
    int sum = 0 ;
    for (int i = 0; i < square.length; i++)
        sum = sum + square[columnNumber][i] ;
    return sum ; 
}

public static int sumRow(int[][] square, int rowNumber)
{
    int sum = 0 ;
    for (int i = 0; i < square[0].length; i++)
        sum = sum + square[i][rowNumber] ;
    return sum ;
}

Check here a working DEMO.

ADD ON's

SAFE CHECKING: as long as you can give any columnNumber or rowNumber the methods can throw an IndexOutOfBounds exception. SOLUTION
To avoid this, check if the given index in inside the array.

HUMANIZING: To sum first row or column you must give 0 value. So in order to make user refer to rows and columns in a human way (column 1 if the first one) SOLUTION
To avoid this use given row / column - 1 in your methods AND checks:

public static int sumColumn(int[][] square, int columnNumber) {
    // humanize
    columnNumber = columnNumber - 1;
    // safe check
    if ((columnNumber) > square.length) 
        return 0;

    int sum = 0 ;
    for (int i = 0; i < square.length; i++)
        sum = sum + square[columnNumber][i] ;
    return sum ; 
}

public static int sumRow(int[][] square, int rowNumber)
{
    // humanize
    rowNumber = rowNumber - 1;
    // safe check
    if ((rowNumber) > square[0].length) 
        return 0;

    int sum = 0 ;
    for (int i = 0; i < square[0].length; i++)
        sum = sum + square[i][rowNumber] ;
    return sum ;
}
Community
  • 1
  • 1
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
  • Thanks for your quick reply :) Yeah, getting rid of both 'int columnNumber' and 'int rowNumber' would solve the problem. Unfortunately, per my assignment's rules, I cannot change the heading of any methods (including main method) grrr I HAVE to use int columnNumber and rowNumber in my methods ): I'm struggling to incorporate these two variables into both their respective methods as they seem 'redundant'. –  Aug 31 '15 at 10:04
  • Nice code! But my matrix can range from a 2*2 matrix to an infinite number @_@. Parameter must be in such a way where it would sum up the rows individually for any given number of N. –  Aug 31 '15 at 10:37
  • that is what my code does, if no, please [check the demo](https://ideone.com/2b5S4R).... you can insert there any 2d array you need to sum with a column, what my code does not check is valid values of `rowNumber` and `columnNumber` so if you insert a 5x5 array and try to sum column 6 will throw `IOOBException` – Jordi Castilla Aug 31 '15 at 10:39
  • Hmmm, its different for my code. I have edited my question to show you what I mean. I'm not sure how to exactly explain it in words. > –  Aug 31 '15 at 11:13
  • did you checked the demo? do you know how to get the value that a method returns? why you say *its different for my code* ??? your code must have 2 methods to sum a given column or row... that's what my 2 last methods do.... if you don't understand something just say it... but **this does not work** or **in my code is different** are not giving us any clue about your problem. – Jordi Castilla Aug 31 '15 at 11:55
  • Yes I checked the demo. After looking more closely, I tweaked around with the method that was receiving the sums. The program seems to be working as intended for now. Thanks for all your help :)) –  Aug 31 '15 at 13:36
  • great to hear that @Khai. English is not my mother language and I sometimes have dificulties. As I suspect this is a homework... just one more tip... make your methods safe checked as in the last update. – Jordi Castilla Aug 31 '15 at 13:44
  • After checking add on... And as this is your first StackOverflow question you must know: if this or any answer has solved your question please consider [accepting it](http://meta.stackexchange.com/q/5234/179419) by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. – Jordi Castilla Aug 31 '15 at 13:50
  • Oh yes, I forgot. Sorry and thanks for the reminder. –  Sep 01 '15 at 13:01