canada eh

Oct 19, 2011, 10:26 PM

Hi, I am trying to find the mode in an array. Whenever I run the following code the mode only displays 0. The original array is ( mark[] ) it is only configured for a maximum of 10 numbers. Could some one point out to me where I went wrong?

Thanks :)

// create array of tallies, all initialized to zero

int[] tally = new int[101];

for (int i = 0; i < 10; i++) {

tally[i] = 0;

}

// for each array entry, increment corresponding tally box

for (int i = 0; i < 10; i++) {

int value = (int) mark[i];

tally[value]++;

}

// now find the index of the largest tally - this is the mode

int maxIndex = 0;

for (int i = 1; i < 10; i++) {

if (tally[i] > tally[maxIndex]) {

maxIndex = i;

}

}

System.out.println("Mode is: "+ maxIndex);

Thanks :)

// create array of tallies, all initialized to zero

int[] tally = new int[101];

for (int i = 0; i < 10; i++) {

tally[i] = 0;

}

// for each array entry, increment corresponding tally box

for (int i = 0; i < 10; i++) {

int value = (int) mark[i];

tally[value]++;

}

// now find the index of the largest tally - this is the mode

int maxIndex = 0;

for (int i = 1; i < 10; i++) {

if (tally[i] > tally[maxIndex]) {

maxIndex = i;

}

}

System.out.println("Mode is: "+ maxIndex);