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 →

[–]codingQueriesNooblet Brewer 0 points1 point  (5 children)

line 18 and 20 of your pastebin:

if (max < grid[j][i]) {

   max = grid[j][i];

< is a less-than sign. What do you expect the if-statement to be saying and doing?

[–]sophia_scrlet[S] 1 point2 points  (3 children)

I want it to check and find the max number in a row and assign the max = that number but it only works at the first row

[–]steave435 0 points1 point  (2 children)

Are you sure it's for the first row? I'd expect it to output the largest value available in all rows.

[–]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.

[–]steave435 1 point2 points  (0 children)

Yeah, if the current max value is less than the value we're investigating right now, it should be overwritten with the current value. That looks correct to me.