Results 1 to 4 of 4
  1. #1
    Hexbomber is offline Member
    Join Date
    Dec 2009
    Posts
    2
    Rep Power
    0

    Default Working With Frequency Arrays...

    So I have a hobby project I'm working on, where I have read in 100 000 random numbers between 1 and 50.
    I have stored them in a frequency array of int's, (aptly named freq[]). So basically every time I read in
    the number X, I would go freq[X]++; so therefore freq[X] is the number of times X occured.

    .:.

    I want to now pick out the 5 numbers that were generated the most frequently, and the 5 numbers that were generated
    the least frequently.... Any ideas on how I would do this?

    So basically I've created the following method:

    Java Code:
    public static void getMostCommonNumbers(int [] freq)
    {
         int [] mostFrequent;
    
         /* INSERT CODE HERE */
    
         System.out.print("\nThe numbers that appeared the most often were: ");
         
         for(int k = 0; k < mostFrequent.length; k++)
         {
    	System.out.print(mostFrequent[k] + " ");
         }
    
         System.out.println();
    }

    Any help is much appreciated in this :).

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    send that array into a method that sorts. once sorted just take the first and last 5 elements from it.
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  3. #3
    wtd_nielsen is offline Member
    Join Date
    Dec 2009
    Posts
    24
    Rep Power
    0

    Default

    hmm you could instead of using a int array, you could make an array that can contain a class that has a number and a frequence attribute (make a new class). Then when you run the getMostCommonNumbers method, you could make a new array that is sorted by frequence...just an idea..

  4. #4
    Hexbomber is offline Member
    Join Date
    Dec 2009
    Posts
    2
    Rep Power
    0

    Default

    I figured it out, by using nested for's... here is my solution, which works....
    Java Code:
    public static void getMostCommonNumbers()
    {
    		int count = 6;
    
    		int [] high = new int[count];
    
    	  	for (int i = 0; i < count; ++i)
    	  	{
    			high[i] = -1;
      	  	}
    
    	  	for (int i = 0; i < numFreq.length; ++i)
    	  	{
    	    	for (int j = 0; j < count; ++j)
    	     	{
    	     		if (high[j] != -1 && numFreq[i] > numFreq[high[j]])
    	     		{
    	        		for (int k = count - 1; k > j; --k)
    	        		{
    						high[k] = high[k - 1];
    					}
    
    	        		high[j] = i;
    	        		break;
    	      		}
    
    	      		else if (high[j] == -1)
    	      		{
    	        		high[j] = i;
    	        		break;
    	      		}
    	  	}
    }

Similar Threads

  1. Sort Frequency
    By ScaryJello in forum New To Java
    Replies: 7
    Last Post: 03-26-2009, 01:47 AM
  2. Word Frequency
    By capu in forum Advanced Java
    Replies: 2
    Last Post: 10-09-2008, 03:03 PM
  3. Java ME beep(frequency, time)
    By Morfmor in forum New To Java
    Replies: 2
    Last Post: 08-28-2008, 11:34 PM
  4. Frequency Counter
    By justlearning in forum New To Java
    Replies: 0
    Last Post: 05-07-2008, 11:50 PM
  5. [SOLVED] BST Frequency Counter
    By theonly in forum Advanced Java
    Replies: 7
    Last Post: 04-30-2008, 12:33 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
  •