Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Help Fix Java Two Dimensional Array Error. The code is supose to get a two dimensional array of 9x9 ints, have a verification that it

Help Fix Java Two Dimensional Array Error.

The code is supose to get a two dimensional array of 9x9 ints, have a verification that it is a solution for a Sudoku puzzle, must return TRUE, and print out a line saying "It works!"

public class array { static int[][] grid = { {4, 2, 6, 5, 7, 1, 3, 9, 8}, {8, 5, 7, 2, 9, 3, 1, 4, 6}, {1, 3, 9, 4, 6, 8, 2, 7, 5}, {9, 7, 1, 3, 8, 5, 6, 2, 4}, {5, 4, 3, 7, 2, 6, 8, 1, 9}, {6, 8, 2, 1, 4, 9, 7, 5, 3}, {7, 9, 4, 6, 3, 2, 5, 8, 1}, {2, 6, 5, 8, 1, 4, 9, 3, 7}, {3, 1, 8, 9, 5, 7, 4, 6, 2} }; private boolean checkSudokuStatus(int[][] grid) { for (int i = 0; i < 9; i++) {

int[] row = new int[9]; int[] square = new int[9]; int[] column = grid[i].clone();

for (int j = 0; j < 9; j ++) { row[j] = grid[j][i]; square[j] = grid[(i / 3) * 3 + j / 3][i * 3 % 9 + j % 3]; } if (!(validate(column) && validate(row) && validate(square))) return false; } return true; for(return true) { System.out.println("Matrix works!"); } if(return false) { System.out.println("Matrix doesnt work"); }

}

private boolean validate(int[] check) { int i = 0; for (int number : check) { if (number != ++i) return false; } return true; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Marketing Database Analytics

Authors: Andrew D. Banasiewicz

1st Edition

0415657881, 978-0415657884

More Books

Students also viewed these Databases questions