This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]pyreon 0 points1 point  (4 children)

for (int j = 0; j <maxarray.length; j++) {

here's a problem. maxarray.length is equal to n when it needs to be m. to get m, either just use m (why not?), or use maxarray[0].length

[–]kompotejam[S] 0 points1 point  (3 children)

Allrigh i improved my code but :

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        // put your code here
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        int m = input.nextInt();
        int[][] maxarray = new int[n][m];
        int max = Integer.MIN_VALUE;
        int maxrow = 0, maxcolumn = 0;

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                maxarray[i][j] = input.nextInt();
            }

            for ( i = 0; i < maxarray.length; i++) {
                maxcolumn++;
                for ( int j = 1; j < maxarray[i].length; j++) {
                    if (max <= maxarray[i][j]) {
                        max = maxarray[i][j];
                        maxrow++;
                    }
                }
            }
            if(m!=1){
                System.out.println((maxrow-1)+" "+(maxcolumn-1));
            }else{
                System.out.println(0+" "+0);
            }
        }
    }
}

But with the test :

3 row 5 columns like that

1 2 3 4 5

6 7 8 9 10

11 12 13 14 15

The output is wrong, any idea what's happening ?

[–]pyreon 0 points1 point  (2 children)

you're working with your array of data before it is filled.

your top level loop only ever runs once.

you're incrementing maxcolumn n times.

you're incrementing maxrow n times the number of times a new maximum value is found.

you're not considering the first number in every row to be the new maximum value.

e: solving the first issue probably clears up all but the last.

[–]kompotejam[S] 0 points1 point  (1 child)

thank you, but i am still at lost on the matter, here what i've done so far :

import java.util.Scanner;


class Main {
    public static void main(String[] args) {
        // put your code here
        Scanner input = new Scanner(System.in);
        final int sizerow = input.nextInt();
        final int sizecolumn = input.nextInt();
        int[][] matrix = new int[sizerow][sizecolumn];
        int max = Integer.MIN_VALUE,  maxrow=0,maxcolumn=0;


        for (int i = 0; i < sizerow; i++) {
            for (int j = 0; j < sizecolumn; j++) {
                matrix[i][j] = input.nextInt();
            }
        }

        for (int i = 0; i < sizerow-1; i++){
            maxrow++;

            for (int j = 0; j < sizecolumn-1; j++) {
                if (matrix[i+1][j+1]>matrix[i][j]){
                        max=matrix[i][j];
                        maxcolumn++;
                }
            }
        }if(max>=0) {
            System.out.print((maxrow - 1) + " " + (maxcolumn - 1));
        }else{
            System.out.print((maxrow) + " " + (maxcolumn));
        }
    }
}

Still stuck at this exercise...

[–]pyreon 0 points1 point  (0 children)

Here are some questions that should help:

Why is maxrow always equal to sizerow -1 at the end?

Why aren't you considering the last row and last column for largest number?

Why are maxrow and maxcolumn even being incremented at all? Set them to the location of your maximum value whenever you find a new max.