Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Scanner input = new Scanner (System.in);
- double[][] matrix=new double [3][4];
- int x, y;
- System.out.println("Enter the dimentions of the array: ");
- x = input.nextInt();
- y = input.nextInt();
- for (int i=0; i<x; i++){
- for (int t=0; t<y; t++){
- matrix [i][t]=input.nextDouble();
- }
- }
- int[] location = locateLargest(matrix); //Here we create an integer array called loaction and set it to what locateLargest() returns
- System.out.println("the location of the largest element of you two-dimensional array is : (" + location[0] + ", " + location[1] + ")");
- }
- public static int[] locateLargest(double[][] a){
- int[] location={0,0};
- double largest=a[0][0];
- for (int i=0;i<a.length;i++){ //a.length only gets the length of the first dimention
- for (int t=0;t<a[1].length;t++){ //using a[1].lengthe gets the length of the second dimension
- System.out.println("X: "+i+" Y: "+t +" value: " + a[i][t]);
- if (a[i][t]>largest){
- largest=a[i][t]; //current value is largest
- location[0]=i; //keep track of the index of the largest in the location array
- location[1]=t;
- }
- }
- }
- //System.out.println("the location of the largest element of you two-dimensional array is : (" + location[0] + ", " + location[1] + ")");
- //System.out.println(location[0]+1 + ", " + (location[1]+1) );
- //System.out.println(a[location[0]][location[1]]);
- return location; //This will return an integer array with length 2 containing the index of the largest element
- }
Advertisement
Add Comment
Please, Sign In to add comment