Reputation: 1
int matrice2 [][] = new int [5][5];
for (int i=0;i<5;i++) {
System.out.println(" ");
for(int j=0;j<5;j++) {
matrice2[i][j] = (int)(Math.random()*10);
System.out.print(" "+matrice2[i][j]+" ");
}
}
System.out.println(" ");
System.out.println(" ");
for (int i=0;i<5;i++) {
System.out.println(" ");
for(int j=0;j<5;j++) {
for (int k=0;k<5;k++) {
if(j!=k) {
if (matrice2[i][j]==matrice2[i][k]) {
matrice2[i][k]=(int)(Math.random()*10);
}
}
}
System.out.print(" "+matrice2[i][j]+" ");
}
}
I wanted to make a multidimensional array without having any repeated numbers in the same row so the column doesn't matter if it has repeating numbers.
What I did here is generate 5x5 arrays formed in square. Having index "j" as the one that gets compared with numbers in index "k" that checks the whole row if there are numbers that is equal to the number it contains in index "j". So my only problem is that after detecting the number in "k" that is equal to the number in "j", the number in index "k" will generate a new number replacing the current number in "k" but the result in console shows that the original 5x5 array which is the first formed of the array before it gets changed has been changed but the numbers that have been replaced generated a numbers that already exist even it should keep changing until "k" cannot detect any repeated numbers.
I can think of other ways of doing it but I really want to know why it doesn't work even though I really think that it shouldn't give any problems but I might have missed something.
here's the example of the results that shouldn't have occured.
from
9 1 3 8 4
5 3 2 4 8
9 8 5 6 5
6 3 0 8 7
2 8 6 3 9
to
9 1 3 8 4
5 3 2 4 8
9 8 5 6 9
6 3 0 8 7
2 8 6 3 9
it shouldn't happen because "k" should have seen it since he starts from index 0 to 4.
while "j" is in index 4 and "k" is in index 0 it should have detected it immediately and change the number in "k" to something else random.
EDIT: I see the different ways you guys are showing but what Im asking is to provide maybe a better solution without using any imports. our teacher gave us this assignment telling us without using any of those imports which makes it more complicated but it's what we are asked for. I tried it with while but still no changes happening.
Upvotes: 0
Views: 98
Reputation: 2436
The problem in your code was that after you detected the duplicate value, you assigned the new generated random number without checking if it is also a duplicate number. what i did here is that i detect duplicate and before assigning/replacing the new generated random number i will check whether it is also duplicate or not, if it is duplicate i will generate another otherwise i will assign it and move on.
(int)(Math.random()*10); // this code does not guarantee that new unique number is generated every time you call it.
the problem is in the code below , you are updating matrice2[i][k]
with new (int)(Math.random()*10)
with out checking whether it is same or different number.
if (matrice2[i][j]==matrice2[i][k]) {
matrice2[i][k]=(int)(Math.random()*10);
}
Upvotes: 0
Reputation: 51892
The way I read your logic is that when you find a duplicate you generate a new number and that new number will validated in the next iteration of the outer (j
) loop. The problem is when j==k
because that number will not be validated, normally this isn't a problem since j
will increase and then that number will be validated with the exception of when j==4
since that is the last iteration.
So modify the rightmost column and that value will not be checked because 'j==k' will never be false.
Upvotes: 0
Reputation: 2147
I editted your code in a different way. I wrote some comment to code understand clearly. Try it please.
public class UniqueMatrix {
public static void main(String[] args) {
int matrix[][] = new int[5][5];
boolean uniqeMatrixFound = false;
while (!uniqeMatrixFound) {
//fill matrix until uniqe matrix found value is true
fillMatrix(matrix);
for (int i = 0; i < matrix.length; i++) {
HashSet<Integer> columnNumber = new HashSet<>();
for (int j = 0; j < matrix.length; j++) {
columnNumber.add(matrix[j][i]);
}
//if set size not equal to matrix size , create an new uniqe matrix with breaking false value
if (columnNumber.size() != matrix.length) {
uniqeMatrixFound = false;
break;
}
uniqeMatrixFound = true;
}
}
//print an array
for (int i = 0; i < matrix.length; i++) {
System.out.println(" ");
for (int j = 0; j < matrix.length; j++) {
System.out.print(" " + matrix[i][j] + " ");
}
}
}
//create a matrix with unique value in all rows.
private static void fillMatrix(int[][] matrice2) {
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 10; i++) {
list.add(i);
}
for (int i = 0; i < matrice2.length; i++) {
Collections.shuffle(list);
for (int j = 0; j < matrice2.length; j++) {
matrice2[i][j] = list.get(j);
}
}
}
}
Upvotes: 1
Reputation: 467
Here is a different approach to the problem that uses a shuffled ArrayList instead of checking whether a value exists in the current row or not.
int matrice2[][] = new int[5][5];
ArrayList<Integer> sourceMatrix = new ArrayList<Integer>();
for (int i = 0; i < 10; i++)
sourceMatrix.add(i);
//generate random matrix using shuffled arraylist
for (int i = 0; i < matrice2.length; i++) {
Collections.shuffle(sourceMatrix);
for (int j = 0; j < matrice2[i].length; j++) {
matrice2[i][j] = sourceMatrix.get(j);
}
}
//print generated matrix
for (int i = 0; i < matrice2.length; i++) {
for (int j = 0; j < matrice2[i].length; j++) {
System.out.print(matrice2[i][j]);
}
System.out.println();
}
Upvotes: 0
Reputation: 172
Here is your code edited. I also commented inline what were the changes and why. You could have done it more easy but I think it suites you more to edit your example so you can understand. Only issue is that you should have stored the value on matrice2[i][j] = newValue (to let k go to the end of the line and check with all other values that your random is not an existing number)
Hope it helps.
public class UniqueMatrix {
public static void main (String[] args) {
int matrice2 [][] = new int [5][5];
for (int i=0;i<5;i++) {
System.out.println(" ");
for(int j=0;j<5;j++) {
matrice2[i][j] = (int)(Math.random()*10);
System.out.print(" "+matrice2[i][j]+" ");
}
}
System.out.println(" ");
System.out.println(" ");
for (int i=0;i<5;i++) {
System.out.println(" ");
for(int j=0;j<5;j++) {
for (int k=0;k<5;k++) {
if(j!=k) {
if (matrice2[i][j]==matrice2[i][k]) {
int newValue = (int)(Math.random()*10);
//store the newly found value in j for you have time to check with others till the end
matrice2[i][j] = newValue;
}
}
}
System.out.print(" "+matrice2[i][j]+" ");
}
}
}
}
Upvotes: 0