Results 1 to 7 of 7
Thread: [SOLVED] Array Mode Problem
- 12-09-2008, 02:17 AM #1
Member
- Join Date
- Nov 2008
- Posts
- 4
- Rep Power
- 0
[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.
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.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 } } } }
Any tips or hints to help me go in the right direction?
-
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.
- 12-09-2008, 03:45 AM #3
Member
- Join Date
- Nov 2008
- Posts
- 4
- Rep Power
- 0
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]); } }
-
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.
- 12-09-2008, 04:05 AM #5
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
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
- 12-09-2008, 04:30 AM #6
Member
- Join Date
- Nov 2008
- Posts
- 4
- Rep Power
- 0
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! ;)
-
Similar Threads
-
Finding the Mode in An Array
By carlodelmundo in forum New To JavaReplies: 23Last Post: 10-31-2010, 12:44 PM -
Disable Drag mode of Jinternal Frame
By smartsubroto in forum New To JavaReplies: 4Last Post: 06-23-2008, 07:21 AM -
array problem
By oceansdepth in forum New To JavaReplies: 3Last Post: 04-05-2008, 02:25 AM -
array problem
By wats in forum New To JavaReplies: 1Last Post: 12-12-2007, 07:08 AM -
array problem
By Albert in forum Advanced JavaReplies: 2Last Post: 07-01-2007, 01:13 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks