Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Getting multi mode problem

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Getting multi mode problem

    public class MmM{
    public static void main(String[] args) 
    {
     
    	double[] data = {1, 1, 2, 2, 3 };
     
    	MmM.mode(data);
    }            
             public static void mode(double [ ] numArray)
             {
                    double f = 0;
    		double hF=0;
    		int length = numArray.length;
    		for (int i = 0; i <length; ++i) 
    		{
    			int count = 0;
    			for (int j = 0; j <length; ++j) 
    			{
    				if (numArray[j] == numArray[i])
    				{
    				++count;
     
    				}	
    			}
    			if (count > hF)
    			{
    				hF = count;
    				f = numArray[i];
    			}
    		}
    			System.out.println("Mode = " + f);
    // I am trying to find mode. Out put of these codes is " 1 " 
    // Required: Out put should be " 1 " and " 2 " both, because they both have highest and equal (i.e 2 times ) 
    //numbers of repetitions which is the mode of the given items in the array
    // How do I achieve that both 1 and 2 instead of just 1 in the out put?
     
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Getting multi mode problem

    print all the values that has equal * * frequencies.
    Can you explain what that means?
    What should the program's output look like?

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Getting multi mode problem

    I was trying to say print all the values that has equal number of repetitions, but I didn't see those two * * characters while posting. I am sorry for that.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Getting multi mode problem

    What should the program's output look like?

    The posted code does not compile for testing.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Getting multi mode problem

    Required output is commented at the bottom of the codes. If you already saw that and think it wasn't clear enough, the output should be the mode of the given data. In this case, there should be two modes but the problem is it can produce just one mode. My question is how can I get both modes as output?

    --- Update ---

    I added public class MmM{ ... and changed MmM.stats to MmM.mode it should work now.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Getting multi mode problem

    If there can be more than one mode, then you need a way to save all of them.
    One way could be with an array. Another with collection class like a Map.

    Why is the counter hF double instead of int?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. question about debug mode
    By leonne in forum Java IDEs
    Replies: 5
    Last Post: May 15th, 2013, 06:18 AM
  2. question about debug mode
    By leonne in forum Java Theory & Questions
    Replies: 2
    Last Post: January 9th, 2013, 04:16 PM
  3. mode.addRow with background color
    By nsyncpilu in forum AWT / Java Swing
    Replies: 3
    Last Post: December 15th, 2011, 09:50 AM
  4. [SOLVED] Jython interactive mode
    By helloworld922 in forum Java Theory & Questions
    Replies: 1
    Last Post: July 19th, 2010, 06:39 AM
  5. Works on debug mode but not on run mode
    By alfonsoraul in forum Member Introductions
    Replies: 0
    Last Post: April 14th, 2010, 02:58 PM

Tags for this Thread