Question

In: Computer Science

Lab - Validate all of the row in the puzzle. // //   - Use this code...

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++)
               {
               }

           }
       }
   }

Solutions

Expert Solution


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


Related Solutions

construct A*star algorithm for solving the 8-puzzle problem . Use MATLAB or Python .Your code should...
construct A*star algorithm for solving the 8-puzzle problem . Use MATLAB or Python .Your code should include two heuristic functions -misplaced tiles and calculation of manhattan distance. The code should work for all cases of puzzle.
• This lab, you will write a Java program to determine if a given Sudoku puzzle...
• This lab, you will write a Java program to determine if a given Sudoku puzzle is valid or not. • You determine if the puzzle is complete and valid, incomplete, or is invalid. • A puzzle is a 2-dimensional array 9x9 array. Each element contains the numbers 1 – 9. A space may also contain a 0 (zero), which means the spot is blank. • If you don’t know how a Sudoku Puzzle works, do some research, or download...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with //comments . Please include a screen shot of the output Part 4: Calorie Counting Specifications: Write a program that allows the user to enter the number of calories consumed per day. Store these calories in an integer vector. The user should be prompted to enter the calories over the course of one week (7 days). Your program should display the total calories consumed over...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with //comments . Please include a screen shot of the output Part 1: Largest and Smallest Vector Values Specifications: Write a program that generates 10 random integers between 50 and 100 (inclusive) and puts them into a vector. The program should display the largest and smallest values stored in the vector. Create 3 functions in addition to your main function. One function should generate the...
Draw all possible border pieces of a puzzle, each having a different shape. Border-pieces are puzzle...
Draw all possible border pieces of a puzzle, each having a different shape. Border-pieces are puzzle pieces that have at least one smooth edge.
Like all our lab exercises, the waves and sound lab will use a PhET simulator. Some...
Like all our lab exercises, the waves and sound lab will use a PhET simulator. Some of these simulators require you to enable Java or Flash. You can download and use the simulator on your desktop, or for some labs you can run it in your browser. Preferred browser settings and system requirements can be found at the “Running Sims” FAQ:  https://phet.colorado.edu/en/help-center/running-sims (Links to an external site.) For this lab, we’ll use the “Sound Waves” simulator: https://phet.colorado.edu/en/simulation/legacy/sound (Links to an external...
How to validate Javascript form data? Here is the code. Can someone modify it so that...
How to validate Javascript form data? Here is the code. Can someone modify it so that all the information is validated? Thanks. <!DOCTYPE html> <html lang="en"> <head>    <title>Music Survey</title>    <meta charset="utf-8"> </head> <style>    legend { font-weight:bold;    }    </style> <body> <h1>Music Survey</h1> <form method="post" action=""> <label for="myname"><b>Name:</b></label>        <input type="text" name="myname" id="myname">        <br><br> <label for="myemail"><b>Email:</b></label>        <input type="email" name="myemail" id="myemail">        <br><br>   <fieldset> <legend>Select Your Favorite Types of Music:</legend> <input type="checkbox"...
Write a program to validate Canadian Postal Codes. A postal code must follow the pattern of...
Write a program to validate Canadian Postal Codes. A postal code must follow the pattern of L9L9L9 where: L is a letter 9 is a digit Your program should continue accepting postal codes until the user enters the word “exit”. Sample run (user input is shown in bold underline): Enter a postal code: T2T-3X7 Postal code wrong length, format L9L9L9 Enter a postal code: T2T3AA Postal code has letters where there are supposed to be digits Enter a postal code:...
please give examples of four row equivalent matrices which are all in row echelon forms (not...
please give examples of four row equivalent matrices which are all in row echelon forms (not necessarly reduced row echelon)
Write the DML code to add the first row of each table to the database.
Write the DML code to add the first row of each table to the database.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT