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

• 12-09-2013, 04:24 PM
mattyeend
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:
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
• 12-09-2013, 04:43 PM
Norm
Re: How to program the mode into an array of 10 random numbers
Quote:

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.
• 12-09-2013, 08:00 PM
mattyeend
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
• 12-09-2013, 08:20 PM
Norm
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?
• 12-09-2013, 09:06 PM
jim829
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
• 12-10-2013, 01:42 PM
mattyeend
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'.
• 12-10-2013, 01:59 PM
Norm
Re: How to program the mode into an array of 10 random numbers
Quote:

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.
• 12-10-2013, 05:13 PM
jim829
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