Advertisement
mrScarlett

2d Array difference between numbers

Oct 9th, 2016
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. public class twoDdifference
  2. {
  3. public static void main(String [] args){
  4.  
  5. //populating array, 2d, 3 arrays inside an array
  6. int A [][]={{-22,12,-33},{33,62,21},{54,22,42}};
  7. boolean out =false;
  8. // integer n equal 3(3 values in each array), indexrow and column=0
  9. int N = 3;
  10. int IndexRow=0;
  11. int IndexColumn=0;
  12. String Direction ="";
  13. // integer MINIMUM equals abs value of the first and second values in the first of 3 arrays
  14. int Minimum = Math.abs(A[0][0]-A[0][1]);
  15. // nested loops loop through each array.
  16.  
  17. for (int i=0;i<N;i++){
  18. for (int j=0;j<(N-1);j++){
  19. //starting with the first array i=0, j=0
  20.  
  21. System.out.println((A[i][j])+" "+(A[i][j+1]));
  22. if (Math.abs(A[i][j]-A[i][j+1])<Minimum){
  23. Minimum=Math.abs(A[i][j]-A[i][j+1]);
  24. //recording the index row and column of the values with the smallest difference
  25. IndexRow=i;
  26. IndexColumn=j;
  27. Direction="ROWS";
  28. }
  29.  
  30. }
  31. }
  32.  
  33. for (int j=0;j<N;j++){
  34. for (int i=0;i<(N-1);i++){
  35. System.out.println((A[i][j])+" "+(A[i+1][j]));
  36. if ((Math.abs(A[i][j]-A[i+1][j])<Minimum)){
  37. Minimum=Math.abs(A[i][j]-A[i+1][j]);
  38. IndexRow=i;
  39. IndexColumn=j;
  40. Direction="Columns";
  41. }
  42. }
  43. }
  44. System.out.println("Distance"+ Minimum);
  45.  
  46. if (Direction == "Rows"){
  47. System.out.println("Between Element "+ IndexRow+" "+ IndexColumn+" and element "+ IndexRow+" "+ IndexColumn+1 );
  48. }else{
  49. System.out.println("Between Element "+ IndexRow+" "+ IndexColumn+" and element "+ IndexRow+1+" "+ IndexColumn );
  50. }
  51. }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement