ForumsProgramming Forum**Java** Finding Mode in a random set of integers in an Array

23 14990
Graham
offline
Graham
8,051 posts
Nomad

AAAGH! i always get an out of bounds exception error and when i put on a stopping mechanism i get mode = 0. Help?

size = size of list

public void computeMode()
{
while(found = false)
{
for(modeLookUp = 1; modeLookUp < size; modeLookUp++)
for(modeLookUp2 = 0; modeLookUp2 < size; modeLookUp2++)
{
if(list[modeLookUp] == list[modeLookUp2]);
{
mode = list[modeLookUp];
found = true;
}
if(list[modeLookUp] != list[modeLookUp2])
found = false;
}

  • 23 Replies
plasmafish
offline
plasmafish
252 posts
Nomad

Is there any more code supporting that?

NakedNinja
offline
NakedNinja
32 posts
Nomad

Question: why are some of your boolean statements set up with a variable setter, rather than a regular boolean setter? I'm not sure about your code... are there any other classes that you are leaving out? Any methods?

Also, you should get Eclipse. It points out your errors immediately, offers suggestions to improve the flow of your code, and does most of the constructor/menial work for you.

Hectichermit
offline
Hectichermit
1,828 posts
Bard

Doesn't an array need a Constant variable for its size, Haven't used Java in a While but what about declaring variable types such as int and boolean...

Graham
offline
Graham
8,051 posts
Nomad

sorry had 1 minute before bell and even the teacher couldn't explain why it wouldn't work.

The whole code uses a previous set class called Expo for random and scanner, it was already in so why change it?



import java.util.Arrays;


public class Lab12MATH080
{

public static void main(String args[])
{
System.out.println("\
Lab12MATH08\
&quot;
System.out.print("Enter the quantity of random numbers ===>> &quot;
int listSize = Expo.enterInt();
System.out.println();
Statistics intList = new Statistics(listSize);
intList.randomize();
intList.computeMean();
intList.computeMedian();
intList.computeMode();
intList.displayStats();
System.out.println();
}
}


class Statistics extends Lab12MATH080
{

private int list[];
private int size;
private double mean;
private double median;
private double medianIndex;
private double medianLowIndex;
private double medianHighIndex;
private int mode;
private int modeLookUp;
private int modeLookUp2;
private boolean found = false;

public Statistics(int s)
{
size = s;
list = new int[s];
}

public void randomize()
{
Expo.startSeed(12345);
for (int k = 0; k < size; k++)
{
int rndNumber = Expo.seedRandom(10,30);
list[k] = rndNumber;
}
}

public void computeMean()
{
for(int i = 0; i < size; i++)
{
mean += list[i];
}
mean /= size;
}


public void computeMedian()
{
Arrays.sort(list);
if(size % 2 == 0)
{
medianLowIndex = (size-1)/2.0;
medianHighIndex = (size+1)/2.0;
median = (list[(int)medianLowIndex] + list[(int)medianHighIndex]) / 2.0;
}
else
{
medianIndex = (size-1)/2.0;
median = list[(int)medianIndex];
}


}

public void computeMode()
{
while(found = false)
{
for(modeLookUp = 1; modeLookUp < size; modeLookUp++)
for(modeLookUp2 = 0; modeLookUp2 < size; modeLookUp2++)
{
if(list[modeLookUp] == list[modeLookUp2]);
{
mode = list[modeLookUp];
found = true;
}
if(list[modeLookUp] != list[modeLookUp2])
found = false;
}
}
}

public void displayStats()
{
System.out.println(Arrays.toString(list));
System.out.println();
System.out.println("Mean: " + mean);
System.out.println("Median: " + median);
System.out.println("Mode: " + mode);
}

}





NakedNinja
offline
NakedNinja
32 posts
Nomad

Why is there an improper escape sequence in your print statement?

System.out.println("\\Lab12MATH08\\&quot;
Graham
offline
Graham
8,051 posts
Nomad

\
is a quick way to do a line in println


there's nothing wrong with the syntax, it's just a logic error. When i put

public void computeMode()
{
for(modeLookUp = 1; modeLookUp < size; modeLookUp++)
for(modeLookUp2 = 0; modeLookUp2 < size; modeLookUp2++)
{
if(list[modeLookUp] == list[modeLookUp2]);
{
mode++;
}
}
}


it gives me the correct number of equal numbers. All i need is a way to println the actual index value.

ex:
http://i271.photobucket.com/albums/jj146/grahaam/array.png

see there's two 17's so the mode = 1 when it has mode++.

I need to get it to say 17, but when i put mode = list[modeLookUp]; i get an arrays out of bounds exception error. when i put a stop to the for loops with a while and boolean I always get mode = 0

NakedNinja
offline
NakedNinja
32 posts
Nomad

Uhh... you got me there. But what type of compiler are you using? Cause in "my" Java, to move to the next line, you need to do the escape sequence, which instead of "\\" is "\
".
Either way, i was looking for syntax errors. I didn't know what you were trying to find.

Graham
offline
Graham
8,051 posts
Nomad

agg, in AG postings it goes to next line! it's

http://i271.photobucket.com/albums/jj146/grahaam/n.png

using JCreator LE fo this.

Hectichermit
offline
Hectichermit
1,828 posts
Bard

All i need is a way to println the actual index value.


if(list[modeLookUp] == list[modeLookUp2]);
{
mode = list[modeLookUp];
found = true;
System.out.print(list[modeLookUp])
}


Dunno if that will work but you don't have to separate printing functions from math functions...
Hectichermit
offline
Hectichermit
1,828 posts
Bard

Srry dont think you need the a few lines

if(list[modeLookUp] == list[modeLookUp2]);
{
found = true;
System.out.print(list[modeLookUp])
}

I was wondering how are you stopping the for loop...it isn't being told to stop so it will run through the entire array and change the mode value...Dunno maybe its 0 because your going out of bounds of the array...the last number that is indexed, so you get an error your function stops and it returns a 0

Graham
offline
Graham
8,051 posts
Nomad

I was wondering how are you stopping the for loop


with the while. the while process goes until found is equal to true.

i think it's the nested for loops that's the problem. The class is tomorrow, i may have to just use a different method with linear searches.
Parsat
offline
Parsat
2,180 posts
Blacksmith

HecticHermit: There's a flaw with that, I think, because if you do that, every time it goes through the array to check the mode it will print out whatever number correlates, while I think Graham just wants it printed out once at the end.

Graham, the problem with the first set of code is that if you have multiple repeated numbers, mode will equal whatever the final repeated number is, whether it is the mode or not. If our array had {0, 1, 1, 1, 0}, your original code would end up with mode=0.

Your second program I presume has mode=0 at the beginning, and when a number is found to match it will add 1 to mode. Problem is, if you use {0, 1, 1, 1, 0} as your array, mode=10 by the time you're through with the code.

Here's a corrected write of your code. I have added the variables "incidences," which acts as a counter for the number of times a number appears in the array, "maxIncidences," which stores the greatest number of repeated values, and "x," which acts as a disposable value for each value in the array. What it does is that it will start with the first value in the array and record the number of incidences of the value through the whole array. Then, it will check to see whether this incidence is larger than the greatest incidence thus far. If it is, maxIncidence is given the value of the new incidence, and the new mode is the value in that array. Then, it will head to the second value, rinse and repeat.

public void computeMode()
{
while(found = false)
{
for (modeLookUp = 0; modeLookUp < size; modeLookUp++)
{
int incidences = 0;
for (modeLookUp2 = 0; modeLookUp2 < size; modeLookUp2++)
{
if (list[modeLookUp2] == list[modeLookUp1])
incidences++;
}
if (incidences > maxIncidences)
{
maxIncidences = incidences;
mode = list[modeLookUp];
}
}
found=true;
}
}

Hectichermit
offline
Hectichermit
1,828 posts
Bard

Well he also wants the number that repeats to be printed out...I wasnt working on it long...havent programed anything in a while and Im still a beginner anyway..Parsat you program? didnt know that....maybe I should start something....

Parsat
offline
Parsat
2,180 posts
Blacksmith

I'm actually learning Java right now. I know some TI-BASIC as well, but overall I'm a novice. Taking AP Computer Science A right now.

Printing it out is simple as a adding a System.out.println anyway.

Graham
offline
Graham
8,051 posts
Nomad

it always seems so easy looking at the code, yet so hard coming up with it.

i'll try it tomorrow.

Showing 1-15 of 23