In: Computer Science
Lab - Validate all of the row in the puzzle.
//
// - Use this code as a start of the program.
// - Catch all duplicate entries in each row.
// - Catch any numbers that are not 1 - 9.
// - Display an error msg for each error found.
// - At end, display a msg stating how many errors were
found.
//=====================================================================
import java.util.*;
public class Lab_ValidateAllRows
{
public static void main (String[] args)
{
int puzzle[][] =
{
{ 1, 2, 3, 4, 5,
6, 7, 8, 9 },
{ 4, 5, 6, 7, 8,
9, 1, 2, 3 },
{ 7, 8, 9, 1, 2,
3, 4, 5, 6 },
{ 2, 3, 4, 5,
6, 7, 8, 9, 1 },
{ 5, 6, 7, 8, 9,
1, 2, 3, 4 },
{ 8, 9, 1, 2, 3,
4, 5, 6, 7 },
{ 3, 4, 5, 6,
7, 8, 9, 1, 2 },
{ 6, 7, 8, 9, 1,
2, 3, 4, 5 },
{ 9, 1, 2, 3, 4,
5, 6, 7, 8 }
};
int row, col, i;
for (row = 0; row < 9;
row++)
{
for (col = 0;
col < 9; col++)
{
}
}
}
}
import java.util.*;
public class Lab_ValidateAllRows
{
// for sorting user define function
static int sortRowWise(int m[][])
{
// loop for rows of matrix
for (int i = 0; i < m.length; i++) {
// loop for column of matrix
for (int j = 0; j < m[i].length; j++) {
// loop for comparison and swapping
for (int k = 0; k < m[i].length - j - 1; k++) {
if (m[i][k] > m[i][k + 1]) {
// swapping of elements
int t = m[i][k];
m[i][k] = m[i][k + 1];
m[i][k + 1] = t;
}
}
}
}
/* printing the sorted matrix.if you want to display the sorting
array
* Uncomment the logic for printing sorted array.
* for (int i = 0; i < m.length; i++) { for (int j = 0; j <
m[i].length; j++)
* System.out.print(m[i][j] + " "); System.out.println(); }
*/
return 0;
}
public static void main (String[] args)
{
int puzzle[][] =
{
{ 1, 2, 3, 4, 5, 6, 7, 8, 9 },
{ 4, 5, 6, 7, 8, 9, 1, 2, 3 },
{ 7, 8, 9, 1, 2, 3, 4, 5, 6 },
{ 2, 3, 4, 5, 6, 7, 8, 9, 1 },
{ 5, 6, 7, 8, 9, 1, 2, 3, 4 },
{ 8, 9, 1, 2, 3, 4, 5, 6, 7 },
{ 3, 4, 5, 6, 7, 8, 9, 1, 2 },
{ 6, 7, 8, 9, 1, 2, 3, 4, 5 },
{ 9, 1, 2, 3, 4, 5, 6, 7, 8 }
};
/*this array for finding the indexes of duplicated values.this is
copy of puzzle array.
* because when we find the duplicate values we need to sort the
array.so puzzle array indexes are changed.
* so we need to create temp(for holding original array so we find
the original index of duplicate values
*/
int temp[][]=new int[9][9];
// here k ansd y is ordinary variable .this is used for looping
counter
for(int k=0;k<9;k++)
{
for(int y=0;y<9;y++)
{
temp[k][y]=puzzle[k][y];
}
}
// first we need to sort the array for finding duplicate element
.so i create a userdefined function sortrowwise()
int row, col,totalerr=0; // here the total error variable is for
counting the total error
sortRowWise(puzzle); // here the array is sorted
for (row = 0; row < 9; row++)
{
for (col=0;col<8; col++)
{
//in output, counting of row should be start with 1(not zero for
user understanding)
if(puzzle[row][col]==puzzle[row][col+1])
{
String elmentindexs="";
for(int index=0;index<9;index++)
{
if(temp[row][index]==puzzle[row][col])
{
elmentindexs=elmentindexs+" "+index;
}
}
System.out.println("duplicate
element(name):"+puzzle[row][col]+" in row:"+(row+1)+" at
indexs:"+elmentindexs+"");
totalerr++;
}
}
// counting, of row should be start with 1(not zero for user
understanding)
for(int i=0;i<9;i++)
{
int elmentindexs=0;
for(int index=0;index<9;index++)
{
if(temp[row][index]==puzzle[row][i])
{
elmentindexs=elmentindexs+index;
}
}
if((puzzle[row][i]>9||puzzle[row][i]<0))
{
System.out.println("Element is not beetween 1 to 9
:"+puzzle[row][i]+"(element name) in row: "+(row+1)+" at index
"+elmentindexs);
totalerr++;
}
}
}
//total Errors (including duplicate no error + greater than no is
between (1-9))
System.out.println("The totol Errors is found :"+totalerr);
}
}
screenshots