This article describes one of the ways to find the adjacent elements for a particular element in a given square Matrix.
For Example:
String[] A = ["10#20#30", "40#50#60", "70#80#90"]
Its a 3x3 matrix whose order is 3.
Eg1: The adjacent or Neighboring Elements of 50 are
10 20 30
40 50 60
70 80 90
Here, 50 is surrounded by all other elements.
Eg2: The adjacent or Neighboring Elements of 20 are
10 20 30
40 50 60
Eg3: The adjacent or Neighboring Elements of 30 are
20 30
50 60
Eg4: The adjacent or Neighboring Elements of 40 are
10 20
40 50
70 80
Based on the element 's position, we consider it as either a starting element (S), a Middle element(M) or an End element (E).
Eg:
10, 70 are starting elements.
20, 40, 50, 60, 80 are Middle elements.
30, 90 are Ending elements.
Following is code for printing adjacent elements of a square matrix. Please execute to understand it.
Need pass two parameters
1st param: Order of the matrix: eg: input1=3
2nd param: String of Rows, where each element of the array is a row. All elements of a row are delimited by # symbol.
Eg:
input2=['row1','row2','row3']
row1='10#20#30' row2='40#50#60' row3='70#80#90'
Download Code: AdjacentElements.java
The Adjacent Elements of 5x5 matrix are printed as :
String[] input2=
{"12#45#33#27#23", "94#54#23#53#11", "98#59#27#62#12",
"11#51#63#13#46", "17#45#31#78#67"};
Output
For Example:
String[] A = ["10#20#30", "40#50#60", "70#80#90"]
Its a 3x3 matrix whose order is 3.
Eg1: The adjacent or Neighboring Elements of 50 are
10 20 30
40 50 60
70 80 90
Here, 50 is surrounded by all other elements.
Eg2: The adjacent or Neighboring Elements of 20 are
10 20 30
40 50 60
Eg3: The adjacent or Neighboring Elements of 30 are
20 30
50 60
Eg4: The adjacent or Neighboring Elements of 40 are
10 20
40 50
70 80
Based on the element 's position, we consider it as either a starting element (S), a Middle element(M) or an End element (E).
Eg:
10, 70 are starting elements.
20, 40, 50, 60, 80 are Middle elements.
30, 90 are Ending elements.
Following is code for printing adjacent elements of a square matrix. Please execute to understand it.
Need pass two parameters
1st param: Order of the matrix: eg: input1=3
2nd param: String of Rows, where each element of the array is a row. All elements of a row are delimited by # symbol.
Eg:
input2=['row1','row2','row3']
row1='10#20#30' row2='40#50#60' row3='70#80#90'
Download Code: AdjacentElements.java
public class AdjacentElements {
public static void main(String args[]) {
int input1=5;
String[] input2={"12#45#33#27#23", "94#54#23#53#11", "98#59#27#62#12","11#51#63#13#46","17#45#31#78#67"};
/*
Few Examples you can try
String[] input2={"12#45#33#27", "94#54#23#53", "98#59#27#62","11#51#63#13"};
String[] input2={"12#45#33", "94#54#23", "98#59#27"};
String[] input2={"12"};
String[] input2={"12#45", "94#54"};
* You need pass two parameters
* first parameter (input1): Order Of Square Matrix;
* Second parameter (input2): Arrays of Rows where elements of the each row delimited by a # symbol.
* Eg: ['row1','row2','row3'...]
*/
printNeighboringElements(input1, input2);
}
public static void printNeighboringElements(int input1, String[] input2){
if(input1!=1){
String[] tempRows={};
for(int i=0;i < input2.length;i++){
/*
* Here, It prints adjacent elements of all the elements
* of any square matrix of any order.
* We assume element's position in the matrix as any of these 3 values
* S or M or E
* S represents Starting Element
* M represents Middle Element
* E represents Ending Element.
*/
String[] reqRow1Elements=new String[input1];
String[] reqRow2Elements=new String[input1];
String[] reqRow3Elements=new String[input1];
String rowStatus = findRowOrPosStatus(i,input1);
if(rowStatus=="S"){
tempRows = new String[2];
tempRows[0]=input2[i];
tempRows[1]=input2[i+1];
reqRow1Elements=tempRows[0].split("#");
reqRow2Elements=tempRows[1].split("#");
}else if(rowStatus=="M"){
tempRows = new String[3];
tempRows[0]=input2[i-1];
tempRows[1]=input2[i];
tempRows[2]=input2[i+1];
reqRow1Elements=tempRows[0].split("#");
reqRow2Elements=tempRows[1].split("#");
reqRow3Elements=tempRows[2].split("#");
}else if(rowStatus=="E"){
tempRows = new String[2];
tempRows[0]=input2[i-1];
tempRows[1]=input2[i];
reqRow1Elements=tempRows[0].split("#");
reqRow2Elements=tempRows[1].split("#");
}
String[] rowElements = input2[i].split("#");
for(int e=0;e < rowElements.length;e++){
System.out.println("Adjacent/Neighboring Elements of "+rowElements[e]);
System.out.println("***********************************");
String posStatus=findRowOrPosStatus(e, input1);
if(posStatus=="S"){
if(rowStatus=="S" || rowStatus=="E"){
System.out.println(reqRow1Elements[e]+" "+reqRow1Elements[e+1]);
System.out.println(reqRow2Elements[e]+" "+reqRow2Elements[e+1]);
}else if(rowStatus=="M"){
System.out.println(reqRow1Elements[e]+" "+reqRow1Elements[e+1]);
System.out.println(reqRow2Elements[e]+" "+reqRow2Elements[e+1]);
System.out.println(reqRow3Elements[e]+" "+reqRow3Elements[e+1]);
}
}else if(posStatus=="M"){
if(rowStatus=="S" || rowStatus=="E"){
System.out.println(reqRow1Elements[e-1]+" "+reqRow1Elements[e]+" "+reqRow1Elements[e+1]);
System.out.println(reqRow2Elements[e-1]+" "+reqRow2Elements[e]+" "+reqRow2Elements[e+1]);
}else if(rowStatus=="M"){
System.out.println(reqRow1Elements[e-1]+" "+reqRow1Elements[e]+" "+reqRow1Elements[e+1]);
System.out.println(reqRow2Elements[e-1]+" "+reqRow2Elements[e]+" "+reqRow2Elements[e+1]);
System.out.println(reqRow3Elements[e-1]+" "+reqRow3Elements[e]+" "+reqRow3Elements[e+1]);
}
}else if(posStatus=="E"){
if(rowStatus=="S" || rowStatus=="E"){
System.out.println(reqRow1Elements[e-1]+" "+reqRow1Elements[e]);
System.out.println(reqRow2Elements[e-1]+" "+reqRow2Elements[e]);
}else if(rowStatus=="M"){
System.out.println(reqRow1Elements[e-1]+" "+reqRow1Elements[e]);
System.out.println(reqRow2Elements[e-1]+" "+reqRow2Elements[e]);
System.out.println(reqRow3Elements[e-1]+" "+reqRow3Elements[e]);
}
}
}
}//rows loop
}else {
System.out.println("No adjacent elements-Its a 1X1 matrix");
}//for 1X1 matrix
}
private static String findRowOrPosStatus(int rowOrPos,int input1){
String rowOrPosStatus="";
if(rowOrPos==0){
rowOrPosStatus="S";
}else if(rowOrPos==input1-1){
rowOrPosStatus="E";
}else if(rowOrPos < input1-1 && rowOrPos!=0){
rowOrPosStatus="M";
}
return rowOrPosStatus;
}
}
The Adjacent Elements of 5x5 matrix are printed as :
String[] input2=
{"12#45#33#27#23", "94#54#23#53#11", "98#59#27#62#12",
"11#51#63#13#46", "17#45#31#78#67"};
Output
Adjacent/Neighboring Elements of 12 *********************************** 12 45 94 54 Adjacent/Neighboring Elements of 45 *********************************** 12 45 33 94 54 23 Adjacent/Neighboring Elements of 33 *********************************** 45 33 27 54 23 53 Adjacent/Neighboring Elements of 27 *********************************** 33 27 23 23 53 11 Adjacent/Neighboring Elements of 23 *********************************** 27 23 53 11 Adjacent/Neighboring Elements of 94 *********************************** 12 45 94 54 98 59 Adjacent/Neighboring Elements of 54 *********************************** 12 45 33 94 54 23 98 59 27 Adjacent/Neighboring Elements of 23 *********************************** 45 33 27 54 23 53 59 27 62 Adjacent/Neighboring Elements of 53 *********************************** 33 27 23 23 53 11 27 62 12 Adjacent/Neighboring Elements of 11 *********************************** 27 23 53 11 62 12 Adjacent/Neighboring Elements of 98 *********************************** 94 54 98 59 11 51 Adjacent/Neighboring Elements of 59 *********************************** 94 54 23 98 59 27 11 51 63 Adjacent/Neighboring Elements of 27 *********************************** 54 23 53 59 27 62 51 63 13 Adjacent/Neighboring Elements of 62 *********************************** 23 53 11 27 62 12 63 13 46 Adjacent/Neighboring Elements of 12 *********************************** 53 11 62 12 13 46 Adjacent/Neighboring Elements of 11 *********************************** 98 59 11 51 17 45 Adjacent/Neighboring Elements of 51 *********************************** 98 59 27 11 51 63 17 45 31 Adjacent/Neighboring Elements of 63 *********************************** 59 27 62 51 63 13 45 31 78 Adjacent/Neighboring Elements of 13 *********************************** 27 62 12 63 13 46 31 78 67 Adjacent/Neighboring Elements of 46 *********************************** 62 12 13 46 78 67 Adjacent/Neighboring Elements of 17 *********************************** 11 51 17 45 Adjacent/Neighboring Elements of 45 *********************************** 11 51 63 17 45 31 Adjacent/Neighboring Elements of 31 *********************************** 51 63 13 45 31 78 Adjacent/Neighboring Elements of 78 *********************************** 63 13 46 31 78 67 Adjacent/Neighboring Elements of 67 *********************************** 13 46 78 67
No comments:
Post a Comment