4x4 matrix array help
Hello guys, im new to the forum I searched around on the net and this seems like one of the best for programming help. Im currently a sophmore in college, a computer science major, and am enrolled in CSCI 1302. In doing this latest project I have encountered a problem. The project is to have a 4x4 matrix, populate the array with numbers 1-15, but to leave one space in the matrix blank(16 places, 1 blank), its also known as somthing called the fiften puzzle. Anyway I wrote the code to take a 1 dimensional array and randomly populate with numbers 1-15 having no duplicates. Now I need to either change my array to a 4x4, or put the current randomized data into a new 4x4 array. This is where im getting stuck, as im not really sure how to do this...ive checked the book and on the net to try and figure this out, but no luck there. This is the code I have now:
----------------------------
import java.util.Random;
public class Arrays2
{
private static final int MAX = 15;
private static Random rand ;
private static int [] array2;
private static int [] test;
public static void main(String mystrings [])
{
rand = new Random();
array2 = new int[MAX];
test = new int[MAX];;
boolean finished = false;
int i = 0;
for(i=0; i < test.length; i++)
test[i] = array2[i]= 0;
////////////////////////////
i = 0;
int count =0;
while ( ! finished)
{
count++;
array2[i] = Math.abs(rand.nextInt() % 15) + 1;
test[array2[i]-1]++;
if(test[array2[i]-1] > 1 )
continue;
i++;
if( i >= array2.length)
finished = true;
}
////////////////////////////////////
for( i = 0; i < array2.length; i++)
{
System.out.println(array2[i]);
}
}//END OF main()
}//END OF class Arrays
----------------------------
I also attached the code in a txt file in case that is easier to read. Anyway, any help turning this array into a 4x4 matrix leaving one space blank would be greatly appreciated. Thanks in advance guys!!
# 1 Re: 4x4 matrix array help
Use the code below to declare your array and create it. Now, instead of one number from 1 to 15, you will need two number from 0 to 3.
private static int[][] array2;
array2 = new int[3][3];
# 2 Re: 4x4 matrix array help
I tried to do this before, but once I change this it loads the program with errors. Maybe im putting in the wrong code or putting it in the wrong spot or somthing. Maybe I need to take out somthing once I change it to this, but when I try to put that I get tons of errors.
# 3 Re: 4x4 matrix array help
What sort of errors are you getting? I'm assuming you have made changes to your code to use the 2D data structure. For example, the loop that sets all entries to zero will now have to be a double for loop. etc.
# 4 Re: 4x4 matrix array help
ahh, there would lie my problem. I only created the double array, didnt change the rest of the program. Okay so for the first loop which reads:
----------------
for(i=0; i < test.length; i++)
test[i] = array2[i]= 0;
------------------
I need to make this a double for loop, would I do somthing like this?:
---------------
for(i=0; i < test.length; i++)
for(int j = 0; j <test.length;i++)
test[i][j] = array2[i][j]= 0;
----------------
Not quite sure how to do that, so after that im thinking I would go through and anywhere in the program I have array2[i] I would need to change it to array2[i][j]?? Also the line: array2 = new int[MAX];....do I need to get rid of that, or is it fine? Because I still need the max number to be 15, but that line of code I dont think will work with a 2d array. Heh as you can tell, not too great with 2d arrays, mostly only done work with 1ds. Thanks for all your help, hopefully soon ill have this thing going.
# 5 Re: 4x4 matrix array help
I'm confused (which is normal for me I guess) what do you mean by "Leave one blank" do you mean leave it initialized to zero?
# 6 Re: 4x4 matrix array help
lol not sure, Its supposed to be a 4x4 matrix, 16 squares....numbers 1-15, so where number 16 should be is in place a blank square.
# 7 Re: 4x4 matrix array help
Well , to that end here is one solution I came up with. It doesn't seem to elegant to me but it works for what (I think) you are trying to do. Basically fill a List<Integer> with numbers 0 - 15, shuffle the array using the Collections.shuffle() method, and then add the numbers, in their now random order to the int[][]. When you print it out, just skip over the 0 value. Like I said, not elegant, but maybe you can refine the logic. If you need code examples, let me know.
Garrett
# 8 Re: 4x4 matrix array help
gmrowe, could you give a code example for the "then add the numbers, in their now random order to the int[][]. " section. I think I got the rest, but a little fuzzy about that.
# 9 Re: 4x4 matrix array help
Here's some code, your variable names are confusing so I'm going to assume:
randomArray is the array of the numbers 0 to 15 in a random order
targetGrid is the result array
int counter = 0;
for(int i = 0; i <= 3; i++)
{
for(int j = 0; j<=3;j++)
{
targetGrid[i][j] = randomArray[counter];
counter++;
}
}
# 10 Re: 4x4 matrix array help
Here's some code, your variable names are confusing so I'm going to assume:
randomArray is the array of the numbers 0 to 15 in a random order
targetGrid is the result array
int counter = 0;
for(int i = 0; i <= 3; i++)
{
for(int j = 0; j<=3;j++)
{
targetGrid[i][j] = randomArray[counter];
counter++;
}
}
Just forget the "Confusing variable names" in the last post. For what you have wirten, if randomArray contains numbers 0 - 15 in random order, then thats almost exactly how I wrote it. So now you have a 4 x 4 matrix populated with 0 - 15 in random order. The question now is how do you get the element corresponding to the number 0 to appear "blank". As I eluded to earlier, I think it depends on what you mean by blank.
# 11 Re: 4x4 matrix array help
int counter = 0;
for(int i = 0; i <= 3; i++)
{
for(int j = 0; j<=3;j++)
{
targetGrid[ i ][ j ] = randomArray[counter];
counter++;
}
}
The standard way to control a loop through an array is to loop while the index is less than the length of the array. This is less prone to error and a bit more self-documenting. For a 2D array, it goes like this:int counter = 0;
for(int i = 0; i < randomArray.length; i++)
{
for(int j = 0; j < randomArray[0].length; j++)
{
targetGrid[ i ][ j ] = randomArray[counter];
counter++;
}
}Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live...
M. Golding
dlorde at 2007-11-10 2:31:20 >

# 12 Re: 4x4 matrix array help
dlorde of course is right. Except he accidently put randomArray int the body of the loop instead of targetGrid. And the body of the inner loop should read:
for(int j = 0; j < targetGrid[i].length; j++)
beacause this works when the 2D array is not symmetrical.
# 13 Re: 4x4 matrix array help
dlorde of course is right. Except he accidently put randomArray int the body of the loop instead of targetGrid.Oh yes, my mistake - well spotted - the loop max length should be taken from targetGrid rather than randomArray in each case.
And the body of the inner loop should read:
for(int j = 0; j < targetGrid[ i ].length; j++)
beacause this works when the 2D array is not symmetrical.
My apologies, I thought we were dealing with a 4x4 matrix :rolleyes:
Walking on water and developing software from a specification are easy if both are frozen...
E. Berard
dlorde at 2007-11-10 2:33:16 >

# 14 Re: 4x4 matrix array help
thanks a ton for your help guys! This is a great place for guys like me to learn, and you were all very friendly and helpful!