Results 1 to 7 of 7
Thread: Simple array questions
- 02-15-2009, 02:35 AM #1
Senior Member
- Join Date
- Nov 2008
- Posts
- 105
- Rep Power
- 0
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
Anyone got any ideas?Java 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; }
On this input:
0 0 7 0 0 28
Your code said: false
The correct result: trueLast edited by jigglywiggly; 02-15-2009 at 02:38 AM.
- 02-15-2009, 02:45 AM #2
thats because cnt will never be > data.length.
you have to use two counters. one for if data[i] ==0,
another for !=0.USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 02-15-2009, 02:50 AM #3
Senior Member
- Join Date
- Nov 2008
- Posts
- 105
- Rep Power
- 0
Oh yeah lol, thanks.
-
Nothing to do with Java and all to do with pure logic. Come on now:
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.Java Code:if(cnt>data.length)
- 02-15-2009, 02:52 AM #5
Senior Member
- Join Date
- Nov 2008
- Posts
- 105
- Rep Power
- 0
Fixed if anyone cares
Java 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, 04:17 AM #6
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)
Hope this helped.Java 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;
-MK12Tell me if you want a cool Java logo avatar like mine and I'll make you one.
- 02-15-2009, 05:57 AM #7
Member
- Join Date
- Feb 2009
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
I have Questions -_-
By ChazZeromus in forum New To JavaReplies: 5Last Post: 09-13-2008, 08:08 PM -
Help with a very simple method for a very simple beginner.
By cakeman in forum New To JavaReplies: 2Last Post: 05-04-2008, 05:27 PM -
2 simple java questions
By jimJohnson in forum New To JavaReplies: 2Last Post: 02-02-2008, 09:35 AM -
questions about using array to store profile
By hien_NU in forum New To JavaReplies: 6Last Post: 01-08-2008, 05:03 AM -
3 Questions
By hiranya in forum AWT / SwingReplies: 4Last Post: 11-14-2007, 04:57 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks