# Thread: Help. Create 2D array method that returns index of row that contains the most zeros.

1. Member
Join Date
Aug 2011
Posts
2
Rep Power
0

## Help. Create 2D array method that returns index of row that contains the most zeros.

I need help creating a 2D array method that returns the index of row that contains the most zeros. I have created a method to count the zeros in each row first. I inserted this method into the other method. I have tried many different things and nothing seems to work. Currently I tried to have the max number of zeros outputted. This doesn't work either. I would like to have the index displayed.

Java Code:
```public class P118
{

public static void main(String[]args)
{

int[][]num = {{0,3,9}, {1,3,0}, {0,9,9}, {0,0,7}};

System.out.print(rowWithMostZeros(num));

}

public static int rowWithMostZeros(int[][]arr)
{
int count=0, count2=0, rowNum = -1, max=0;

for(int row = 0; row<arr.length-1;row++)
{

count = countZeros(arr[row]);

for(int i = 1;i<arr.length; i++)
{
count2 = countZeros(arr[i]);

if(count>count2)
{
max=count;

}

}
}

return max;
}
public static int countZeros(int[]x)
{
int count = 0;

for(int i = 0; i<x.length;i++)
{
if(x[i]==0)
{
count++;

}
}

return count;
}
}```
Last edited by pbrockway2; 08-21-2011 at 01:39 AM.

2. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,537
Rep Power
11
Welcome to the forums. When you post code use the "code" tags. You put [code] at the start of the code and [/code] at the end: that way the code is readable when it appears here. There is a # button in the panel where you compose your post that will insert the tags for you around selected text.

It is a good idea to explain what "doesn't work" means. In particular, does the code compile? If not, and you can't understand the compiler's message, post the message.

If the code does compile, what happens when you run it?

-----

In these sorts of problem it is a very good idea to begin with a complete and precise "recipe": an explanation in ordinary language of the steps you are going to obtain to get the output you want. I presume the output is supposed to be 3, because I recognise that the last element contains the most zeros. But what precise steps do I use to determine that? In particular I only had to examine each element once, so I don't think a "recipe", were I to make it precise, would involve nested loops.

 Also you have a rowNum variable which is never used. I don't mention this to be critical of a well intentioned attempt: but it further suggests that you should stop and formulate a "plan of attack" before you write the code.
Last edited by pbrockway2; 08-21-2011 at 01:51 AM.

3. Member
Join Date
Aug 2011
Posts
2
Rep Power
0
Thanks for the reply. The program does compile. The problem is that I am getting an incorrect result. I would like to get 3 for the output. I used the nested for loop because I wanted to compare "count" and "count2" to each other in every possible combination.

Here is what I attempted:

1.Total the zeros in each row using the countZeros() method.
2.Compare "count" and "count2" to each other.
3.If count is greater than count2 set max = count.
4.Obtain the index of the row with the most zeros.

4. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,537
Rep Power
11
I used the nested for loop because I wanted to compare "count" and "count2" to each other in every possible combination
OK. As I said before, when I looked at the num array to see what the answer should be, I worked down the array counting how many zeros were in an entry and keeping track of the biggest count I had seen and where I had seen it. The important thing is that this involved no "back tracking", so I can see no reason for comparing the number of zeros in an element with the number in all of the others as is done in the nested loop.

I suspect the recipe you give is too closely linked with the code. In particular you introduce terms like "count" and "count2" without any description of what they mean.

Imagine yourself before a stack of pieces of paper. Describe how you, yourself, would determine which piece of paper held the largest number of zeros. Assuming that the countZeros() method does what it says your description can contain the expression "count the number of zeros on a page", but other than that it should be an algorithm that is complete, precise and understandable by anyone - without them having to ask: what do you mean by "max", or similar.

I realise that thinking critically about a task that is, in itself, rather simple, is not easy when you first attempt it. But that's the point of the exercise.