Thread: How to program the mode into an array of 10 random numbers

1. Member
Join Date
Dec 2013
Posts
3
Rep Power
0

How to program the mode into an array of 10 random numbers

I'm trying to make a program that has 10 random numbers generated in an array, and to print the 10 numbers out, along with the sum, mode, median, and the mean. However, I have made a program that does the 10 random numbers, the sum, median and the mean.
The part that I am struggling with it is the mode section.
The code that I have done for the mode is:
Java Code:
```    public static double calculateMode(int[] array)
{
double mode = 0;
int maxValue;
int maxCount;
for(int i = 0; i<array.length; i++)
{
int count = 0;
for(int j = 0; j = array.length; j++)
{
if(array[j] == array[i]) count++;
}
if(count > maxCount)
{
maxCount = count;
maxValue = array[i];
}
}
return mode;
}```
And help with this would be greatly appreciated
Last edited by mattyeend; 12-09-2013 at 07:58 PM.

2. Re: How to program the mode into an array of 10 random numbers

am struggling with it is the mode section.
Define what the program needs to do in the mode section. What does the posted code do and what is wrong with its results?
[code]
[/code]
to get highlighting and preserve formatting.

3. Member
Join Date
Dec 2013
Posts
3
Rep Power
0

Re: How to program the mode into an array of 10 random numbers

It needs to print the mode of 10 randomly generated numbers. It comes up with an error that states that the 'maxCount' in the line that holds the code 'if(count > maxCount)' isn't utilized

4. Re: How to program the mode into an array of 10 random numbers

Where does the program assign any value to maxCount before it is used in the if statement?

5. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,501
Rep Power
9

Re: How to program the mode into an array of 10 random numbers

First, look at your for statement. Second, how do you cater to more than one maximum? A set of numbers may have more than one mode if two or more numbers occur the same amount of times.

Regards,
Jim

6. Member
Join Date
Dec 2013
Posts
3
Rep Power
0

Re: How to program the mode into an array of 10 random numbers

I've assigned a value to the maxCount statement. However, when I run it now, there's an error on the line 'System.out.println("Mode = " + maxValue' which says that it can't find the symbol. The arrow that appears is under the 'm' of 'maxValue'.

7. Re: How to program the mode into an array of 10 random numbers

can't find the symbol.
That means that the compiler can not find a definition of that symbol that is in scope (defined within same pair of {}s) where it is being used.

8. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,501
Rep Power
9

Re: How to program the mode into an array of 10 random numbers

Did you read my note in #5? Your for syntax is off. Otherwise, your code worked fine for me (except that it only finds a single mode, not multiple).

Regards,
Jim