# Simple array questions

• 02-15-2009, 03:35 AM
jigglywiggly
Simple array questions
Well I took a break from java even though I am a beginner and now my memory is a bit lacking, I forget things quick.

Anyway, here is the question

Create a method called majorityZeros, which takes an integer array as an input parameter. It should check to see if the array contains more zeros than non-zero values. If the array contains strictly more zeros, then majorityZeros should return true. Otherwise majorityZeros should return false.
Examples:
data:{0,4,7,0} ==> false
data:{0,4,7,0,0} ==> true
data:{0,4,7,0,1,3,3} ==> false

So I went ahead and wrote this
Code:

```public boolean majorityZeros (int[] data) { boolean tobereturned=false; int cnt = 0; int j = data.length; for(int i = 0; i<data.length; i++) {   if(data[i]==0) {       cnt++;     } }  if(cnt>data.length) {  tobereturned=true;  } return tobereturned;   }```
Anyone got any ideas?
On this input:
0 0 7 0 0 28

The correct result: true
• 02-15-2009, 03:45 AM
angryboy
thats because cnt will never be > data.length.
you have to use two counters. one for if data[i] ==0,
another for !=0.
• 02-15-2009, 03:50 AM
jigglywiggly
Oh yeah lol, thanks.
• 02-15-2009, 03:50 AM
Fubarable
Nothing to do with Java and all to do with pure logic. Come on now:
Code:

` if(cnt>data.length)`
Edit: I'm a little slow with the response. But you don't need to counters here, just one. A divide by two will do fine.
• 02-15-2009, 03:52 AM
jigglywiggly
Fixed if anyone cares
Code:

```boolean tobereturned=false; int cnt = 0; int evil=0; int j = data.length; for(int i = 0; i<data.length; i++) {   if(data[i]==0) {     cnt++;   }else {     evil++;   }     } if(cnt>evil) {   tobereturned=true; } return tobereturned;```
• 02-15-2009, 05:17 AM
MK12
A better approach (in coding, not functionality), would be Fubarable's suggestion:
(RED TEXT: Erase, get rid of)(Green TEXT: things I changed)(Orange:comments)
Code:

```boolean [COLOR="Green"]returnValue[/COLOR]=false; [COLOR="orange"]// changed from tobereturned[/COLOR] [COLOR="Orange"]// to returnValue, describes its use better IMO[/COLOR] int cnt = 0; [COLOR="Red"]int evil=0;[/COLOR] int j = data.length; for(int i = 0; i<data.length; i++) {   if(data[i]==0) {     cnt++;   }[COLOR="red"]else {     evil++;   }[/COLOR]     } if(cnt>[COLOR="Green"]data.lenth / 2[/COLOR]) { [COLOR="DarkOrange"]// changed from evil[/COLOR]   returnValue=true; } return returnValue;```
Hope this helped.
-MK12
• 02-15-2009, 06:57 AM
veens4444
Quote:

Originally Posted by MK12
A better approach (in coding, not functionality), would be Fubarable's suggestion:
(RED TEXT: Erase, get rid of)(Green TEXT: things I changed)(Orange:comments)
Code:

```boolean [COLOR="Green"]returnValue[/COLOR]=false; [COLOR="orange"]// changed from tobereturned[/COLOR] [COLOR="Orange"]// to returnValue, describes its use better IMO[/COLOR] int cnt = 0; [COLOR="Red"]int evil=0;[/COLOR] int j = data.length; for(int i = 0; i<data.length; i++) {   if(data[i]==0) {     cnt++;   }[COLOR="red"]else {     evil++;   }[/COLOR]     } if(cnt>[COLOR="Green"]data.lenth / 2[/COLOR]) { [COLOR="DarkOrange"]// changed from evil[/COLOR]   returnValue=true; } return returnValue;```
Hope this helped.
-MK12

Its correct