Results 1 to 11 of 11
  1. #1
    amro is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Question Strange Arrays behavior

    hello every one
    I'm making a code to calculate the mode (most frequent value or values in an array )
    assuming we have only one mode ,The code I made is giving the lowest value if it is repeated even if the actual mode is the maximum value so i expand the code with adding more lines to do the same but with the array inverted
    by using bubble with < instead of >

    the second part of the code will give the maximum value in the array even if the actual mode is the lowest .

    if the mode (most frequent occurring value ) is some where midway but not the maximum nor the minimum value the code is returning it correctly and any one of the tow parts will solve it

    i combined both codes into one and can not find a way to put IF ELSE statement or something similar to return only one of the tow results (result & result1) and eliminate the the possible wrong return that may be one of them

    2,2,5,5,5,5,5 will return the '2' using the first part:confused: and 5 using the second part

    2,2,2,2,2,2,2,5,5,5 will return 5 as result1 using the second part:confused: and 2 using the second part

    2,2,2,5,5,5,5,5,5,5,9,9,9 will return the 5 correctly for both parts

    tempcount and tempcount1 are used to hold the highest frequency and both will be equal in both cases

    there is something fishy happening at the ends of the array :confused:

    one of the tow output ore both is definitely the mode

    this is my code


    Java Code:
     [COLOR="Blue"]  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];
    		   }
    		}
    		
    		 
    		 this.RevArrange(data1);
    	     int count1=0;
    	     int tempcount1=0;
    	     
             for(int i=1; i<data1.length; i++)
             for(int j=0; j<data1.length-1; j++)
             {
    		     if(data1[j]!= data1[j+1]){count1=0;
    		     }
                 if(data1[j]==data1[j+1])
                 {
             count1++;
             }
               if(count1>tempcount1)
               {
    			 tempcount1 = count1;
    			 result1 = data1[j];
    			 
    		   }
    		 }
    		 
    		 
    		 
    		
    		 JOptionPane.showMessageDialog(null,"one of the following tow values is the mode \n\n" + result+" \n and \n"+ result1 +"\n\nthere are possibly more modes\n\n\n\n\n\n\n"+"TESTING parameters <not data> "+"\n tempcount \n"+ tempcount+"\n tempcount1\n "+ tempcount1+"\n count\n" +count +"\ncount1\n"+count1 );
    		 
    		 
    			 	
    	return result1;//ore result1 if else is needed here
     }
     [/COLOR]
    the class and the methods for ascending and descending sorting :

    Java Code:
     [COLOR="Blue"]import javax.swing.*;
        
        class Statistics {
    		private double data1[];
    		private double data2[];
    		
    
    		private int bn1;
    		private double result;
    		private double result1;
    		
         public   Statistics(double  data[],int bn){  // first constructor
           data1 = data ;
           bn1   = bn ;
       }
       
       public   Statistics(double  data[]){ //second constructor
    	   data1 = data ;
       }
       
       
       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;
            }
            
          
       }
       
       
       public void RevArrange( 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;
            }
            
          
       }[/COLOR]
    what am i doing wrong ???:confused:
    Last edited by amro; 10-31-2010 at 02:25 PM. Reason: code tags added

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

    Default

    To find the mode, or the number of occurrences of an element in an array, the best you've to do is write a separate method passing with the array and the number you want to check or count. In that way it's really easy to customise at any time.

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

    Default

    And also be careful when you are writing your own logics to sort arrays. Using default API methods, Array,sort() and so on. Read the API for more details.

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

    Default

    Thank you Eranga for sorting my code inside the boxes
    the thing i need is a logic to decide passing either result or result1
    if you meant a tow rows tow dimensional array that will hold each data in one row and its corresponding frequency in the other, that will be more than great and useful for even more advanced statistical calculations
    I will try to find and understand them
    regards

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

    Default

    Quote Originally Posted by Eranga View Post
    To find the mode, or the number of occurrences of an element in an array, the best you've to do is write a separate method passing with the array and the number you want to check or count. In that way it's really easy to customize at any time.
    the number is unknown and its possible to have no modes at all
    I'm dealing with variable length array and the array content them self are variables controlled by the user input thus, array length and content are decided within runtime thats why most of errors in this code are runtime errors

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

    Default

    Quote Originally Posted by amro View Post
    Thank you Eranga for sorting my code inside the boxes
    the thing i need is a logic to decide passing either result or result1
    if you meant a tow rows tow dimensional array that will hold each data in one row and its corresponding frequency in the other, that will be more than great and useful for even more advanced statistical calculations
    I will try to find and understand them
    regards
    You may loose the association of key-value pair if you deals with arrays, index is very important. Use a Map, then you can keep the exact associate and manipulations are straightforward.

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

    Default

    Quote Originally Posted by amro View Post
    the number is unknown and its possible to have no modes at all
    I'm dealing with variable length array and the array content them self are variables controlled by the user input thus, array length and content are decided within runtime thats why most of errors in this code are runtime errors
    Variable length array means the size is dynamically changed?

    If so why don't you use an ArrayList?

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

    Default

    Quote Originally Posted by Eranga View Post
    Variable length array means the size is dynamically changed?

    If so why don't you use an ArrayList?
    yes or something like this
    the program don't knew the array length before the user insert the data to a text area where the program will split it and parse into double array and pass it to the methods
    what is the different between an array and an array-list ?? it sounds like an array with arrays as elements instead of single value elements :confused:

    what advantage exactly would array-lists provide ??

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

    Default

    OK I found that i will change the code with arraylist
    i like the idea that it can hold doubles and integers
    i guess the code will dramatically shrink because of this
    thank you Eranga

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

    Default

    Quote Originally Posted by amro View Post
    OK I found that i will change the code with arraylist
    i like the idea that it can hold doubles and integers
    i guess the code will dramatically shrink because of this
    ArrayLists can hold object references, not primitives. And the key advantage is that dynamically you can change the size of the ArrayList by adding and removing elements.

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

Similar Threads

  1. Strange tab key focus traversal behavior in JTable
    By javaexplorer in forum AWT / Swing
    Replies: 0
    Last Post: 05-19-2010, 01:59 PM
  2. Strange behavior with java from command line
    By ocean in forum New To Java
    Replies: 1
    Last Post: 12-07-2009, 01:26 AM
  3. Replies: 6
    Last Post: 09-04-2009, 03:48 AM
  4. Strange behavior with Thread.sleep()
    By Steve11235 in forum Advanced Java
    Replies: 16
    Last Post: 05-04-2009, 06:24 AM
  5. strange refreshing behavior
    By diggitydoggz in forum New To Java
    Replies: 4
    Last Post: 12-27-2008, 05:51 PM

Posting Permissions

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