Hi,

I am new. I did my my best to read the rules first so please pardon me if i missed any.

My qestion is in regards to filling up a matrix diagonally:

"Write a method that accepts a square matrix. Assume that the size of the matrix is declared as an instance constant. Fill the array with successive integers as illustrated below by the case when the size is four.

1 3 6 10
2 5 9 13
4 8 12 15
7 11 14 16

This question is meant to refine your ability to fill in the matrix programmatically, not when you declare the array. So please, do not manually fill in the array when you create it.

The code i worked on so far:

public static void main(String[]args){
          int arrTwo[ ][ ] = new int [4][4];
		
		for(int i = 0; i < arrTwo.length; i++)
        {
            for(int j =0; j < arrTwo[0].length; j++)
           {
            	
           System.out.print(arrTwo[i][j] + " ");
            }
           System.out.println();  
        }	
	}

Thank you for any help.

Interesting problem. So you've got the first part right - getting the size of the array. Since it's promised to be a square matric, you can accept one parameter as input, that being the length of one side. (4, in this case).

So you could generalize this by getting a number from the user and assigning it to a variable, say, "size".

For now we'll stub out the input part of things and say you're getting the input from a method:

public int getSizeFromUser()
{
  return 4;
}

There you go, that can be changed after you get the rest of the thing working. (By putting the method in now, you're making a promise to yourself that you'll work out what really should be in there, even if you haven't already - and if you don't get there, at least it's obvious that you were focusing on the right part, the algorithm part)

Now, you'll use the value from that method to set the size of your matrix:

size = getInputFromUser();
int[][] matrix = new int[size][size];

This amounts to the same code you have in there now - it declares a 4X4 matrix - but it's going to make your life easier if you do it this way, and it captures the fact that the sides are the same.

And you're ready for the fun part: filling in the matrix.

Now you want to stop writing code for a little while and sit down with a couple of sheets of graph paper and try to figure out how you'd want to do this. Try differently-sized matrices, and try different approaches. For example, is it easier to put the numbers in serially, in numeric order, and calculate where each is supposed to go, or is it easier to go along a row or a column and calculate which number goes in the next slot as you come to it.

Feel free to talk your way through the algorithm here, but let's don't talk about code until you know exactly what you want the code to do, step by step.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.