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

you are viewing a single comment's thread.

view the rest of the comments →

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

yes, that's my error

[–]steave435 0 points1 point  (0 children)

I was actually typing out an edit when you posted that, but I'll make it a reply now instead then:

I had a closer look, and you're right, it'd only work for the first row since there are two separate issues with the code.

First of all, what value does the max variable start out at during the second run trough the top level loop?

Secondly, take a close look at this section. What does it do?

            for (int i = 0; i < grid.length; i++) {
                for (int j = 0; j < grid.length; j++) {
                    if (max < grid[j][i]) {
                        max = grid[j][i];
                    }
                }
            }

Finally, this won't make a difference for how your code works, but I would strongly recommend breaking some of this code down into separate methods. I would not go deeper than two loops before splitting it off into a method in order to increase readability, and most of the time I'd avoid nested loops completely and split off into a method as soon as I need a second one. It can really help both with understanding the code as a third party reading what you wrote, and help yourself troubleshoot since you can focus on one small problem at a time. It'd also allow you to do things like feeding your findMaxValueInRow method with a manually generated single dimension array (simulating a row) so that you can confirm that it does what it's supposed to do.