Page 1 of 2 12 LastLast
Results 1 to 20 of 24
  1. #1
    carlodelmundo is offline Member
    Join Date
    Aug 2008
    Posts
    31
    Rep Power
    0

    Default Finding the Mode in An Array

    Hi. My instructor gave us an assignment to calculate the modes in an array. Basically, the array values (not the index) contains the frequencies of the index number (like a histogram). For example, since the value of "10" (the highest number for values) occurs in index numbers 2, 4, and 11, the program must output that the "mode occurs at index numbers 2, 4, and 11." Here's what I have so far:

    Java Code:
    public class Stats
    {
    	public static void main(String[]args)
    	{
    		int[] array = new int [16];
    		array[0] = 0;
    		array[1] = 0;
    		array[2] = 10;
    		array[3] = 5;
    		array[4] = 10;
    		array[5] = 0;
    		array[6] = 7;
    		array[7] = 1;
    		array[8] = 0;
    		array[9] = 6;
    		array[10] = 0;
    		array[11] = 10;
    		array[12] = 3;
    		array[13] = 0;
    		array[14] = 0;
    		array[15] = 1;
    		
    		System.out.println("The maximum is  " + CalculateMaximum(array) + ".");
    	}
    	public static int CalculateMaximum(int[] array)
    	{
    		int maximum = array[0];
    		for (int i = 1; i < array.length; i++)
    		{
    			if ( array[i] > maximum)
    			{
    			maximum = array[i];
    			}
    		}
    		return maximum;
    	}
    }
    So far, I've managed to calculate the maximum (the program outputs 10). My problem is, I can't think of an algorithm that will return the index numbers if the array value is its maximum. That is... what can I do so that whenever the program sees that the maximum number (the mode) is 10, that it returns the index value of the array?

    That was confusing. Let me know if you need clarification. Thanks

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

    Default

    Hint: you could use a second array whose indices are the range of numbers possible in your array, initialize each value to 0,... I'll leave the rest to you.

  3. #3
    carlodelmundo is offline Member
    Join Date
    Aug 2008
    Posts
    31
    Rep Power
    0

    Default

    Fubarable,

    Thanks for that response. Someone told me to use a second array, and you're recommending it too! I'm still a novice... the problem still serves ambiguity. What can we achieve by creating a second array whose indices are the ranges 0-10, and initializing each array value to 0?

    Are we trying to create a search algorithm, and it returns the modal indices for the original array?

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

    Default

    To find the algorithm, walk away from the computer, pull out a few sheets of paper, write down 16 numbers ranging from 0-10 on this paper, and solve the problem on paper only. Then do it again. Once you are familiar with the steps necessary to solve this on paper, it's usually fairly easy to translate this into Java code.

    Good luck.

  5. #5
    pheonix is offline Member
    Join Date
    Apr 2008
    Posts
    42
    Rep Power
    0

    Default

    Usually when I want to solve a maximum, minimum programing thing I use Math.max(the first number, the second number);
    all I have to do is just figure out a way to use it with arrays.
    In your case I would put it inside a loop . using the same array (no need for another). I would need 2 loops instead. so that the first number would stay still while the second number keeps increasing. one way or another it would get the maximum. Now I still didn't try it myself and I'm just giving you an idea. If by any chance you wrote this thread because you were looking for someone to actually write the code and show it to u. I would be happy to show you my way, but if you want to do it on your own, thats fine too :)

  6. #6
    carlodelmundo is offline Member
    Join Date
    Aug 2008
    Posts
    31
    Rep Power
    0

    Default

    Unfortunately, my perseverance and pride forces me to solve the problem on my own with only the suggestions of others as helping tools. Thank you for both of your suggestions--but I'm afraid I need to take it a step backwards in order to be on the same page as you two.

    AFAIK, by giving me code, you'll only give me answers to my problem (and I probably will not learn it as well). In addition, that quality of self-accomplishment is gone. Thank you though!

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

    Default

    by giving me code, you'll only give me answers to my problem (and I probably will not learn it as well). In addition, that quality of self-accomplishment is gone.
    Well spoken. If only more felt like you, I'd have more faith in the future of programming.

  8. #8
    pheonix is offline Member
    Join Date
    Apr 2008
    Posts
    42
    Rep Power
    0

    Default

    Good answer bro. I thought u might be just a guy who is taking a required course even though its not your major. Good thing I just gave an idea. :)

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    What index you want to have if if multiple max values are found? According to your example value 10 is the maximum and it found three times in the array. So what is the required index? First occurrence or all of them?

  10. #10
    khajalid is offline Member
    Join Date
    Sep 2008
    Posts
    31
    Rep Power
    0

    Default

    what can you do is that just search for number with highest frequency.Then,search again and compare with that no and print the index.

  11. #11
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    Java Code:
    public int[] getMaximum(int[] array) {
    int[] toReturn = new int[array.length+1];
    		for (int i = 0, a = 0; i < array.length; i++)
    		{
    			if ( array[i] > toReturn[0])
    			{
    			toReturn[0] = array[i];
                            toReturn[a++] = i;
    			}
    		}
    		return toReturn;
    	}
    }
    This returns an array with the maximum in the first index, and in the other indexes the place of the it in the array checked.
    I die a little on the inside...
    Every time I get shot.

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by khajalid View Post
    what can you do is that just search for number with highest frequency.Then,search again and compare with that no and print the index.
    Yes, this is the easiest way you have to follow. That's why I ask about the maximum occurrence.

  13. #13
    amro is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default

    Hello
    I am doing a similar assignment.In a previous part of my code I have a method to arrange the data in a variable length array Arrange() .and by modifying the arrange method and add it after performing the arrange we have

    public double Mode( double [] data1 )
    {
    this.Arrange(data1);

    int count=0;
    int tempcount=0;
    for(int i=1; i<data1.length; i++)
    for(int j=0; j<data1.length-1; j++){
    count=0;
    if(data1[j]==data1[j+1]){
    count++;
    if(count>tempcount){
    tempcount = count;
    result = data1[j];
    }
    }
    }

    return result;
    }

    and the original arrange method is :

    public void Arrange( double [] data1 )
    {

    double temp;
    for(int i=1; i<data1.length; i++)
    for(int j=0; j<data1.length-1; j++)
    if(data1[j]>data1[j+1]){
    temp=data1[j];
    data1[j]=data1[j+1];
    data1[j+1]=temp;
    }
    I hope this will help

  14. #14
    amro is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default

    the idea is to compare frequencies and keep the highest which is easy when the array is arranged and it is somehow similar to the bubble arrange it self

  15. #15
    amro is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default

    but what if the data is bimodal (tow modes) or multimodal(three and above)


    this code will only return the mode that will appear first (lowest value mode in ascending order or highest in descending order)
    that means we can use the same code but with reversing the order and compare it with the first mode, if equal we return it.
    but i think its more practical to use only this code inside new loop by starting each time at the index next to the last mode index and compare and and save equal modes

    again it sames more even easier to do it without the new loop thus, by inserting another array to keep the modes
    but this will create more problems for me because i designed the my Jframe to receive only a single double :(
    how to insert the array in this code ??
    any ideas are welcomed and appreciated

    public double Mode( double [] data1 )
    {
    this.Arrange(data1);

    int count=0;
    int tempcount=0;
    for(int i=1; i<data1.length; i++)
    for(int j=0; j<data1.length-1; j++){
    count=0;
    if(data1[j]==data1[j+1]){
    count++;
    if(count>tempcount){
    tempcount = count;
    result = data1[j];
    }
    }
    }
    Last edited by amro; 10-29-2010 at 07:44 PM.

  16. #16
    amro is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default

    any idea how to retrieve the modes and put them in an array to be returned to the caller ??
    Last edited by amro; 10-29-2010 at 10:03 PM. Reason: repeated

  17. #17
    amro is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default rwong

    the above codes by me are wrong
    i will be back after figuring out whats wrong :mad:

  18. #18
    amro is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default

    this new code is working now for finding one mode
    but if there are more than one mode it will return only the one that occur first (lower value)

    public double Mode( double [] data1 )
    {
    this.Arrange(data1);

    int count=0;
    int tempcount=0;
    for(int i=1; i<data1.length; i++)
    for(int j=0; j<data1.length-1; j++){
    if(data1[j]!= data1[j+1]){count=0;}
    if(data1[j]==data1[j+1]){
    count++;}

    if(count>tempcount){
    tempcount = count;

    result = data1[j];}
    }


    return result;
    }

  19. #19
    amro is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default

    the last code will not work probably if the last element in the array is part of a mode and is deciding if it is the mode or not

    fore example 1,1,1,4,4,4,4 will give one as the mode which is wrong
    but 1,1,1,4,4,4,4,4,4 will give the four which is correct because the code knew that before reaching the last element
    this is because of the length-1

  20. #20
    amro is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default

    perhaps we should look for other arrangement methods and modify them to find the mode

    any idea ?

Page 1 of 2 12 LastLast

Similar Threads

  1. Finding the largest number in an array
    By starchildren3317 in forum New To Java
    Replies: 14
    Last Post: 11-03-2010, 06:49 AM
  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. Finding elements in a vector
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:37 PM
  4. Finding largest no
    By bugger in forum New To Java
    Replies: 11
    Last Post: 11-29-2007, 12:49 PM
  5. Finding GCF in java
    By lenny in forum Advanced Java
    Replies: 1
    Last Post: 07-31-2007, 05:41 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
  •