Getting multi mode problem
Code java:
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?
}
}
Re: Getting multi mode problem
Quote:
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.
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.
Re: Getting multi mode problem
What should the program's output look like?
The posted code does not compile for testing.
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.
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?