Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class twoDdifference
- {
- public static void main(String [] args){
- //populating array, 2d, 3 arrays inside an array
- int A [][]={{-22,12,-33},{33,62,21},{54,22,42}};
- boolean out =false;
- // integer n equal 3(3 values in each array), indexrow and column=0
- int N = 3;
- int IndexRow=0;
- int IndexColumn=0;
- String Direction ="";
- // integer MINIMUM equals abs value of the first and second values in the first of 3 arrays
- int Minimum = Math.abs(A[0][0]-A[0][1]);
- // nested loops loop through each array.
- for (int i=0;i<N;i++){
- for (int j=0;j<(N-1);j++){
- //starting with the first array i=0, j=0
- System.out.println((A[i][j])+" "+(A[i][j+1]));
- if (Math.abs(A[i][j]-A[i][j+1])<Minimum){
- Minimum=Math.abs(A[i][j]-A[i][j+1]);
- //recording the index row and column of the values with the smallest difference
- IndexRow=i;
- IndexColumn=j;
- Direction="ROWS";
- }
- }
- }
- for (int j=0;j<N;j++){
- for (int i=0;i<(N-1);i++){
- System.out.println((A[i][j])+" "+(A[i+1][j]));
- if ((Math.abs(A[i][j]-A[i+1][j])<Minimum)){
- Minimum=Math.abs(A[i][j]-A[i+1][j]);
- IndexRow=i;
- IndexColumn=j;
- Direction="Columns";
- }
- }
- }
- System.out.println("Distance"+ Minimum);
- if (Direction == "Rows"){
- System.out.println("Between Element "+ IndexRow+" "+ IndexColumn+" and element "+ IndexRow+" "+ IndexColumn+1 );
- }else{
- System.out.println("Between Element "+ IndexRow+" "+ IndexColumn+" and element "+ IndexRow+1+" "+ IndexColumn );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement