Results 1 to 7 of 7
  1. #1
    Banjo is offline Member
    Join Date
    Nov 2008
    Posts
    4
    Rep Power
    0

    Default [SOLVED] Array Mode Problem

    Hi there.
    I'm stuck on a bit of programming, and not through lack of trying.

    I have an array of students' marks.
    I need to find the modes.

    Java Code:
        public static void mode(int classMarks[])
        {
            int[] marksArray = new int[21]; //creates an array that has an entry for each possible mark
            int[] marks = classMarks;
            
            System.out.println("The mode most frequently recurring mark(s):");
            
            for(int i = 0; i < 21; i++)
            {
             for(int j = 0; j < NUMBER_OF_STUDENTS; j++)
                {
                 if(marksArray[i] == marks[j]) //condition:if the mark value appears in the student marks array
                    {
                    marksArray[i]+= 1; //every mark adds 1 to the corresponding value in the array
                    }
                }
            }
        }
    I just can't figure out how to have multiple modes found and return the values, partially because I'm not sure how many modes to expect since it is based on user input.

    Any tips or hints to help me go in the right direction?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You could always sort the array after it has been filled using java.util.Arrays.sort(marksArray). This can make counting the number of similar marks much easier.

  3. #3
    Banjo is offline Member
    Join Date
    Nov 2008
    Posts
    4
    Rep Power
    0

    Default

    Unfortunately, I'm relying on the numbers of the array items being kept the same for the output.
    I've kept at it and cleaned up the code a little.

    It's compiling, but the output is quite odd.
    Java Code:
     int[] markFreq = new int[21]; //creates an array that has an entry for each possible mark
            int[] marks = classMarks;
            int modeMarkNumber=0;
            
            
            
    
            for(int i = 0; i < marks.length; i++)
             {
                markFreq[marks[i]] += 1;//the mark frequency is increased
             }
            
            for(int j = 1; j < markFreq.length; j++)
                {
                if(markFreq[j] > markFreq[j-1])
                    {
                    modeMarkNumber=j;//stores the greatest frequency in a variable
                    }
                }
          
            System.out.println("The mode is: " + modeMarkNumber);
            
            for(int compVar = 0; compVar < markFreq.length; compVar++)
             {
                 if(compVar == modeMarkNumber)
                 {
                     System.out.println(compVar + "\t" + markFreq[compVar]);
                 }
             }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    It's intriguing but frustrating to us when you state that the output is quite odd, but then don't show it.

    suggestions:
    1) If the output is strange, unusual or not expected, please post it.
    2) We'll have an easier time working with your code if you can post a small compilable and runnable program.
    3) If it were me and this were my program, and I couldn't sort my marks array, I'd make a HashMap<Integer, Integer> frequencyMap, and increment the "Value" part of this map for every "Key" encountered in the marks array. It's kind of what you're doing but more like a "sparse" array then a full markFreq array.
    Last edited by Fubarable; 12-09-2008 at 04:05 AM.

  5. #5
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    i'm pretty sure you're gonna have to sort the array in one way or another. if you're being picky about keeping the order, just sort a copy of the array

  6. #6
    Banjo is offline Member
    Join Date
    Nov 2008
    Posts
    4
    Rep Power
    0

    Default

    Thanks to both of you.

    What I actually did was a dry-run on paper to see what really should go where since it was a bit confused.
    It helped clear things up a lot and it was a lot more effective than what was essentially trial and error!

    Also, thanks for your patience, Fubarable! ;)

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    So then you solved it? If so congrats. Oh, and please mark your thread as "solved".

Similar Threads

  1. Finding the Mode in An Array
    By carlodelmundo in forum New To Java
    Replies: 23
    Last Post: 10-31-2010, 12:44 PM
  2. Disable Drag mode of Jinternal Frame
    By smartsubroto in forum New To Java
    Replies: 4
    Last Post: 06-23-2008, 07:21 AM
  3. array problem
    By oceansdepth in forum New To Java
    Replies: 3
    Last Post: 04-05-2008, 02:25 AM
  4. array problem
    By wats in forum New To Java
    Replies: 1
    Last Post: 12-12-2007, 07:08 AM
  5. array problem
    By Albert in forum Advanced Java
    Replies: 2
    Last Post: 07-01-2007, 01:13 AM

Posting Permissions

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