Results 1 to 8 of 8
  1. #1
    mattyeend is offline Member
    Join Date
    Dec 2013
    Posts
    3
    Rep Power
    0

    Default 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 06:58 PM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default 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?
    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    mattyeend is offline Member
    Join Date
    Dec 2013
    Posts
    3
    Rep Power
    0

    Default 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. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default 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?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,783
    Rep Power
    5

    Default 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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    mattyeend is offline Member
    Join Date
    Dec 2013
    Posts
    3
    Rep Power
    0

    Default 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. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default 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.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,783
    Rep Power
    5

    Default 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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Java random numbers in an array!
    By dharav in forum New To Java
    Replies: 5
    Last Post: 09-13-2012, 08:25 AM
  2. Fill Array with Numbers from Random generator [HELP]
    By s_mxyzptlk in forum New To Java
    Replies: 3
    Last Post: 03-07-2012, 02:06 AM
  3. Array of random numbers ...
    By shane1987 in forum New To Java
    Replies: 31
    Last Post: 11-14-2010, 09:33 PM
  4. Store Random numbers into Array
    By abby0910 in forum New To Java
    Replies: 19
    Last Post: 07-12-2010, 12:59 AM
  5. generating random numbers in a 5x5 array.
    By acidacid in forum New To Java
    Replies: 3
    Last Post: 08-14-2007, 03:44 AM

Tags for this Thread

Posting Permissions

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