0

//Kevin Clement //Week3A Magic Squares

Hey all, doing an introductory assignment to 2dimensional arrays. Below is the code I have done which is pretty much done.

My problem I get is I'm not entirely sure how to print out the array, as well as getting everything to run right with a test method. I get an error out of bounds at the line msq[order][order] = 1;

I apologize if my formatting of question is wrong, still not used to this site. Any help would be great. Thanks!

import java.util.*;

class Magic
{
  private int order;
  int msq[ ][ ];

  public Magic(int size)
  {
     if(size < 1 || size % 2 == 0)
     {
        System.out.print("Order out of range");
        order = 3;
     }
     msq = new int[order][order];
     Build();
     Display();
   }

  public void Build()
   {
     int row = 0;
     int col =0;
    msq[order][order] = 1;

     for(int k = 1; k <= order * order; k++)
     {
        msq[row][col] = k;

        if(row == 0 && col == order -1)
           row++;
        else if(row == 0)
        {
           row = order - 1;
           col++;
        }
        else if(msq[row - 1][col + 1] != 0)
           row++;
        else if(msq[row -1][col + 1] == 0)
        {
           row--;
           col++;
        }
        if(col == order - 1)
        {
           col = 0;
           row--;
        }

     }
  }
  public void Display()
  {

     for(int i = 0; i < order; i++)
     {
        for(int k = 0; k < order; k++)
        {
           System.out.println(msq[i][k] + " ");
        }
     }


  }
}
false
  • 10,264
  • 13
  • 101
  • 209
Kevin Clement
  • 21
  • 1
  • 5
  • You should fix the syntax error on the line `msq[order][(order] = 1;` There is an extra left paren. It doesn't get in the way of an answer, but it is easier to help when we can paste the code directly without having to patch it. Cheers. :) – Ray Toal Sep 15 '11 at 03:50
  • Sorry didn't notice the first time, I fixed it in code. and I'll edit post as well. – Kevin Clement Sep 15 '11 at 18:46

4 Answers4

2

I get an error out of bounds at the line msq[order][order] = 1;

msq = new int[order][order];
// ..
msq[order][order] = 1;

If array size is n, then you need to access the elements from 0 to n-1. There is no nth index. In your case there is no order, order index. It is only from 0 to order-1 and is the reason for array index out of bounds exception.

Mahesh
  • 34,573
  • 20
  • 89
  • 115
2

What is the reason for this condition in the constructor?:

if(size < 1 || size % 2 == 0)
 {
    System.out.print("Order out of range");
    order = 3;
 }

Note that whenever you use a size input that doesn't satisfy the if clause, the variable order is not initialized and defaults to 0. As a result the 2d array has size zero and throws the out of bounds error. If you are trying to use 3 as a default value, then u want to move the line:

order = 3;

before the if block.

Other things to consider: 1. make the order variable final since u don't plan on changing it. Eclipse IDE would warn you about the situation described above if you do so.

or 2. If you are going to default to 3 for the value of order initialize it as such.

private int order = 3

Also you might consider printing a message saying order defaults to three when the condition is not satisfied.

chief
  • 31
  • 5
0

To print a matrix of integers in Java

for (int i = 0; i < order; i++) {
    for (int k = 0; k < order; k++) {
       System.out.printf("%6d", msq[i][k]);
    }
    System.out.println();
 }

The second part of your question is answered by Mahesh.

Ray Toal
  • 86,166
  • 18
  • 182
  • 232
0

msq[order][(order] = 1;

--> here is a syntax error. You have an '('. You get end of bound error because array start from 0 not 1 (which is a mistake every beginner makes) therefore change it to msq[order-1][order-1] = 1;

The answer above is the correct way to print out the array.

GSingh
  • 176
  • 1
  • 8
  • Thanks that -1 part I think fixed it, at least it compiles. I'm somewhat confused on how to call this whole method to display the array within the main method. Any ideas? – Kevin Clement Sep 15 '11 at 05:44