1. Member Join Date
Oct 2010
Posts
45
Rep Power
0

## 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  Reply With Quote

2. ## 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.  Reply With Quote

3. ## 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.  Reply With Quote

4. Member Join Date
Oct 2010
Posts
45
Rep Power
0

## 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  Reply With Quote

5. Member Join Date
Oct 2010
Posts
45
Rep Power
0

##  Originally Posted by Eranga 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  Reply With Quote

6. ##  Originally Posted by amro 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.  Reply With Quote

7. ##  Originally Posted by amro 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?  Reply With Quote

8. Member Join Date
Oct 2010
Posts
45
Rep Power
0

##  Originally Posted by Eranga 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 ??  Reply With Quote

9. Member Join Date
Oct 2010
Posts
45
Rep Power
0

## 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  Reply With Quote

10. ##  Originally Posted by amro 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.  Reply With Quote

11. ## And yes, you can manage your code more clearly than this with ArrayList.  Reply With Quote

#### Posting Permissions

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