Koenrad

eightthirteen

Mar 11th, 2015
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.91 KB | None | 0 0
  1.     public static void main(String[] args) {
  2.                     // TODO Auto-generated method stub
  3.  
  4.         Scanner input = new Scanner (System.in);
  5.         double[][] matrix=new double [3][4];
  6.         int x, y;
  7.  
  8.         System.out.println("Enter the dimentions of the array: ");
  9.         x = input.nextInt();
  10.         y = input.nextInt();
  11.         for (int i=0; i<x; i++){
  12.              for (int t=0; t<y; t++){
  13.                      matrix [i][t]=input.nextDouble();
  14.              }
  15.         }
  16.        
  17.         int[] location = locateLargest(matrix);         //Here we create an integer array called loaction and set it to what locateLargest() returns
  18.         System.out.println("the location of the largest element of you two-dimensional array is : ("  + location[0] + ", " + location[1] + ")");
  19.  
  20.     }
  21.          
  22.     public static int[] locateLargest(double[][] a){
  23.  
  24.        
  25.         int[] location={0,0};
  26.         double largest=a[0][0];
  27.         for (int i=0;i<a.length;i++){               //a.length only gets the length of the first dimention
  28.             for (int t=0;t<a[1].length;t++){        //using a[1].lengthe gets the length of the second dimension
  29.                 System.out.println("X: "+i+" Y: "+t +" value: " + a[i][t]);
  30.                 if (a[i][t]>largest){
  31.                     largest=a[i][t]; //current value is largest
  32.                     location[0]=i;     //keep track of the index of the largest in the location array
  33.                     location[1]=t;
  34.                 }
  35.             }
  36.         }
  37.         //System.out.println("the location of the largest element of you two-dimensional array is : ("  + location[0] + ", " + location[1] + ")");
  38.         //System.out.println(location[0]+1 + ", " + (location[1]+1) );
  39.         //System.out.println(a[location[0]][location[1]]);
  40.         return location;    //This will return an integer array with length 2 containing the index of the largest element
  41.     }
Advertisement
Add Comment
Please, Sign In to add comment