Results 1 to 4 of 4
- 08-21-2011, 01:13 AM #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.
- 08-21-2011, 01:48 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.
[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 01:51 AM.
- 08-21-2011, 02:10 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.
- 08-21-2011, 02:25 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
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 used the nested for loop because I wanted to compare "count" and "count2" to each other in every possible combination
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.
Similar Threads
-
My capture sound method returns empty byte array tho I have mic plugged in?
By Addez in forum New To JavaReplies: 2Last Post: 06-04-2011, 11:52 PM -
Inherited method that returns string
By Xeal Rebad in forum New To JavaReplies: 5Last Post: 05-23-2011, 01:26 PM -
Problems with method which returns result
By new Object() in forum New To JavaReplies: 2Last Post: 12-01-2010, 12:16 PM -
How do I write a method that returns an array where ith element contains a[i] + b[i]?
By CaptainBlood in forum New To JavaReplies: 5Last Post: 10-30-2010, 12:05 AM -
How to create an index for the
By suneelakulkarni in forum LuceneReplies: 0Last Post: 07-13-2009, 03:23 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks