Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By pbrockway2

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

  1. #1
    jlss4e is offline Member
    Join Date
    Aug 2011
    Posts
    2
    Rep Power
    0

    Default 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 02:39 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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.

    [Edit] 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 02:51 AM.

  3. #3
    jlss4e is offline Member
    Join Date
    Aug 2011
    Posts
    2
    Rep Power
    0

    Default

    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. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    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.
    Norm likes this.

Similar Threads

  1. Replies: 2
    Last Post: 06-05-2011, 12:52 AM
  2. Inherited method that returns string
    By Xeal Rebad in forum New To Java
    Replies: 5
    Last Post: 05-23-2011, 02:26 PM
  3. Problems with method which returns result
    By new Object() in forum New To Java
    Replies: 2
    Last Post: 12-01-2010, 01:16 PM
  4. Replies: 5
    Last Post: 10-30-2010, 01:05 AM
  5. How to create an index for the
    By suneelakulkarni in forum Lucene
    Replies: 0
    Last Post: 07-13-2009, 04:23 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •