how would this be put into pseudocode? package matrix_design_pseudocode; import
ID: 3865689 • Letter: H
Question
how would this be put into pseudocode?
package matrix_design_pseudocode;
import java.util.Arrays;
import java.util.Scanner;
public class matrix_design_pseudocode {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int row1, col1, row2, col2;
// input first matrix
System.out.println("Enter first matrix dimenntions: ");
System.out.println("Number of rows: ");
row1 = scan.nextInt();
System.out.println("Number of columns: ");
col1 = scan.nextInt();
System.out.println("Enter "+row1+" rows of matrix with "+col1+" numbers in each row");
int[][] X = new int[row1][col1];
for(int i=0; i<row1; i++){
for(int j=0; j<col1; j++){
X[i][j] = scan.nextInt();
}
}
// input second matrix
System.out.println("Enter second matrix dimenntions: ");
System.out.println("Number of rows: ");
row2 = scan.nextInt();
System.out.println("Number of columns: ");
col2 = scan.nextInt();
System.out.println("Enter "+row2+" rows of matrix with "+col2+" numbers in each row");
int[][] Y = new int[row2][col2];
for(int i=0; i<row2; i++){
for(int j=0; j<col2; j++){
Y[i][j] = scan.nextInt();
}
}
// filling new matrix D (no. of rows and columns will be equal to max rows and max columns of both matrix)
int row = Math.max(row1, row2);
int col = Math.max(col1, col2);
int[][] D = new int[row][col];
for(int j=0; j<row; j++){
for(int k=0; k<col; k++){
if(j==0 && k==0){
continue;
}
int i=0;
int min = 100;
boolean not1, not2;
while(i<row){
// checking line1
if(j-1>=0 && k-1>= 0 && j<col1 && k<col2 && X[i][j]==Y[i][k]){
min = Math.min(min,D[j-1][k-1]);
}
else if(j-1>=0 && k-1>= 0 && j<col1 && k<col2 && X[i][j]!=Y[i][k]){
min = Math.min(min,1+D[j-1][k-1]);
}
not1= true;
not2 = true;
// checking line2
for(int l=0; l<col2 && j<col1; l++){
if(j<col1 && X[i][j] == Y[i][l] ){
not1 = false;
}
}
if(!not1 && j-1>=0){
min = Math.min(min,1+D[j-1][k]);
}
// checking line3
for(int l=0; l<col1 && j<col2; l++){
if(j<col2 && Y[i][j] == X[i][l] ){
not2 = false;
}
}
if(!not2 && k-1>=0){
min = Math.min(min,1+D[j][k-1]);
}
i++;
}
// assigning D[j][k] to minimum of all three lines
D[j][k] = min;
}
}
// printing matrix D
System.out.println("Matrix D is:");
System.out.println(Arrays.deepToString(D));
//calculating minvali & totalDifference
int totalDifference=0;
int minvali;
for(int i=0; i<D.length;i++){
minvali = Integer.MAX_VALUE;
for(int j=0; j<D[0].length; j++){
minvali = Math.min(minvali, D[i][j]);
}
totalDifference += minvali;
}
int thres;
System.out.println("Enter the threshold value: ");
thres = scan.nextInt();
if(totalDifference <= thres){
System.out.println("Images are similar!");
}
else{
System.out.println("Images are different!");
}
}
}
Explanation / Answer
import java.util.Arrays;
import java.util.Scanner;
public class matrix_design_pseudocode {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int row1, col1, row2, col2;
// input first matrix
System.out.println("Enter first matrix dimenntions: ");
System.out.println("Number of rows: ");
row1 = scan.nextInt();
System.out.println("Number of columns: ");
col1 = scan.nextInt();
System.out.println("Enter "+row1+" rows of matrix with "+col1+" numbers in each row");
int[][] X = new int[row1][col1];
for(int i=0; i<row1; i++){
for(int j=0; j<col1; j++){
X[i][j] = scan.nextInt();
}
}
// input second matrix
System.out.println("Enter second matrix dimenntions: ");
System.out.println("Number of rows: ");
row2 = scan.nextInt();
System.out.println("Number of columns: ");
col2 = scan.nextInt();
System.out.println("Enter "+row2+" rows of matrix with "+col2+" numbers in each row");
int[][] Y = new int[row2][col2];
for(int i=0; i<row2; i++){
for(int j=0; j<col2; j++){
Y[i][j] = scan.nextInt();
}
}
// filling new matrix D (no. of rows and columns will be equal to max rows and max columns of both matrix)
int row = Math.max(row1, row2);
int col = Math.max(col1, col2);
int[][] D = new int[row][col];
for(int j=0; j<row; j++){
for(int k=0; k<col; k++){
if(j==0 && k==0){
continue;
}
int i=0;
int min = 100;
boolean not1, not2;
while(i<row){
// checking line1
if(j-1>=0 && k-1>= 0 && j<col1 && k<col2 && X[i][j]==Y[i][k]){
min = Math.min(min,D[j-1][k-1]);
}
else if(j-1>=0 && k-1>= 0 && j<col1 && k<col2 && X[i][j]!=Y[i][k]){
min = Math.min(min,1+D[j-1][k-1]);
}
not1= true;
not2 = true;
// checking line2
for(int l=0; l<col2 && j<col1; l++){
if(j<col1 && X[i][j] == Y[i][l] ){
not1 = false;
}
}
if(!not1 && j-1>=0){
min = Math.min(min,1+D[j-1][k]);
}
// checking line3
for(int l=0; l<col1 && j<col2; l++){
if(j<col2 && Y[i][j] == X[i][l] ){
not2 = false;
}
}
if(!not2 && k-1>=0){
min = Math.min(min,1+D[j][k-1]);
}
i++;
}
// assigning D[j][k] to minimum of all three lines
D[j][k] = min;
}
}
// printing matrix D
System.out.println("Matrix D is:");
System.out.println(Arrays.deepToString(D));
//calculating minvali & totalDifference
int totalDifference=0;
int minvali;
for(int i=0; i<D.length;i++){
minvali = Integer.MAX_VALUE;
for(int j=0; j<D[0].length; j++){
minvali = Math.min(minvali, D[i][j]);
}
totalDifference += minvali;
}
int thres;
System.out.println("Enter the threshold value: ");
thres = scan.nextInt();
if(totalDifference <= thres){
System.out.println("Images are similar!");
}
else{
System.out.println("Images are different!");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.