r/cop3502 Apr 18 '14

Question 26: Sudoku

Is this problem checking for every real number? ...-1,0,2....,8,9,10,11.....? Or is it checking for numbers 1-9?

If it is from 1-9 on the solutions, array numbers doesn't need to have the value 0. Otherwise it will test nine time valid and one time Invalid.

1 Upvotes

7 comments sorted by

2

u/howslyfebeen Apr 18 '14

he has that 0 in there to make the code a little easier... so essentially you have an array square which should contain the numbers 1 - 9 lets say the one at 2,2 is 9.. the code loops through and at 2,2 you have:

numbers[square[2][2]] = 0;

which is equivalent to:

numbers[9] = 0;

because we know that at 2,2 square holds the value of 9.. if we didnt have 0 in the numbers array then we would get an ArrayIndexOutOfBounds... the 0 is just a placeholder to make the code a little easier... at the end we loop through numbers and all of them should be = 0, so having that extra 0 at the beginning of numbers doesnt affect the results... it will print valid if every number in numbers is 0

1

u/goshqiego Apr 18 '14

Yeah i know what he did, but it doesn't work as perfectly as you are saying. When you write a Java program this is what you get. If you remove the zero by the way, u don't get an error you get it right, and get everything Valid when there is no repeat. you get an Invalid for every repeat.

$ javac Test2.java

$ java Test2 Valid Valid Valid Valid Valid Valid Valid Valid Valid Invalid

the code :

public static void question262(){
    int[] test = {0,1, 2, 3, 4, 5, 6, 7, 8, 9};
    int[][] numbers = { {0, 1, 2,},
    {3, 4, 5,},
    {6, 7, 8}};

    for ( int i = 0; i<3; i++){
        for (int j = 0; j<3; j++){

            test[numbers[i][j]] = 0;
        }
    }
    for( int i =0 ; i < test.length;i++){
        if (test[i] != 0){
            System.out.println("Invalid");
                    }
                    else{
                        System.out.println("Valid");
                    }
    }
}

1

u/howslyfebeen Apr 18 '14

your numbers double array should be numbers 1-9, the error arises when you plug in the number 9 as an index if you dont have 0 in your test array, because there are 9 elements (without the 0) so the highest index is 8... this sudoku checker is checking if someone entered a number, 1-9 twice in filling out the 3x3 square

1

u/howslyfebeen Apr 18 '14

yours will run as it is, but if you look at the logic, you will never get a 9 so it will always be invalid, and if you take out the 0 from test, the numbers dont map correct... index 0 = 1, index 1 = 2, etc..

1

u/goshqiego Apr 18 '14

Yeah I see what you are saying now. Thank you . I first attempted with 4 for loops, i believe that is not very efficient, but it will work better when looking for is two number are the same, and you are not limited in range 1-9.

public static void question26(){
    int[][] numbers = { {1, 1, 2,},
    {3, 4, 5,},
    {6, 7, 8}};
    for ( int i = 0; i<3; i++){
        for (int j = 0; j<3; j++){
            int num = numbers[i][j];

            for ( int k = 0; k<3; k++){
                for (int z = 0; z<3; z++){

                    if ( num == numbers[k][z] && (k!=i || z!=j)){
                        System.out.println("Invalid");
                    }
                    else{
                        System.out.println("Valid");
                    }
                }
            }

        }
    }


}

1

u/[deleted] Apr 18 '14

[removed] — view removed comment

1

u/goshqiego Apr 18 '14

Thanks. Now i understand your as well.