Results 1 to 7 of 7
  1. #1
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default 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
    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;
     
    }
    Anyone got any ideas?
    On this input:
    0 0 7 0 0 28

    Your code said: false
    The correct result: true
    Last edited by jigglywiggly; 02-15-2009 at 02:38 AM.

  2. #2
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    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)

  3. #3
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    Oh yeah lol, thanks.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Nothing to do with Java and all to do with pure logic. Come on now:
    Java 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.

  5. #5
    jigglywiggly is offline Senior Member
    Join Date
    Nov 2008
    Posts
    105
    Rep Power
    0

    Default

    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;

  6. #6
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    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)
    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;
    Hope this helped.
    -MK12
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  7. #7
    veens4444 is offline Member
    Join Date
    Feb 2009
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by MK12 View Post
    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)
    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;
    Hope this helped.
    -MK12
    Its correct

Similar Threads

  1. I have Questions -_-
    By ChazZeromus in forum New To Java
    Replies: 5
    Last Post: 09-13-2008, 08:08 PM
  2. Replies: 2
    Last Post: 05-04-2008, 05:27 PM
  3. 2 simple java questions
    By jimJohnson in forum New To Java
    Replies: 2
    Last Post: 02-02-2008, 09:35 AM
  4. questions about using array to store profile
    By hien_NU in forum New To Java
    Replies: 6
    Last Post: 01-08-2008, 05:03 AM
  5. 3 Questions
    By hiranya in forum AWT / Swing
    Replies: 4
    Last Post: 11-14-2007, 04:57 AM

Posting Permissions

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