# Arrays - Mode/Median Problems

Page 1 of 1

## 6 Replies - 10604 Views - Last Post: 20 February 2010 - 03:38 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=157118&amp;s=79ab1e5bfe6ea7a9292c9eb283f78f4b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 michaelgates

Reputation: 0
• Posts: 10
• Joined: 22-January 10

# Arrays - Mode/Median Problems

Posted 19 February 2010 - 08:56 PM

Hello, I know there are a lot of post that have to do with this topic and I've looked at as many as I could so that I would not be reposting a problem that was already answered, but I still can't seem to fix my problem. This is obviously an assignment that deals with arrays and calculating different things from a list of numbers. When I look through each line of code, every thing seems to be right, but when I run it I get some problems. Basically my ComputeMedian() method's problem is that when I call it, it returns 0.0 and on top of that, it turns every number in the array into a 0. My ComputeMode() method just returns 0, the rest of the list is stays the way it was. I can't figure out what is going on. Any help would be greatly appreciated. Thanks in advanced.

```    public static double ComputeMedian() {
Arrays.sort(ScoreList);  //Sort the list
int middle = ScoreList.length / 2;  // middle number
if (ScoreList.length % 2 == 1) {
// There is an odd number of test scores so return the one middle number
return ScoreList[middle];
} else {
// There is an even number of test scores so return the average of the middle two
return (ScoreList[middle - 1] + ScoreList[middle]) / 2.0;
}
}
```

```    public static int ComputeMode() {
// Create array of tallies, all initialized to zero
int[] tally = new int[101];
for (int i = 0; i < tally.length; i++) {
tally[i] = 0;
}

// For each array entry, increment the corresponding tally
for (int i = 0; i < ScoreList.length; i++) {
int value = ScoreList[i];
tally[value]++;
}

// Find the index of the largest tally
int maxIndex = 0;
for (int i = 1; i < tally.length; i++) {
if (tally[i] > tally[maxIndex]) {
maxIndex = i;
}
}
return maxIndex;
}
```

On a slightly less important side note, does anybody have any idea why this method to fill the list with random numbers would be not working? It doesn't add any numbers to the list.

```    public static void Random(int amount) {
Random gen = new Random();
for (int i = 0; i < amount + 1; i++) {
ScoreList[i] = (int)gen.nextInt(101);
}
}
```

Is This A Good Question/Topic? 0

## Replies To: Arrays - Mode/Median Problems

### #2 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12267
• Posts: 45,362
• Joined: 27-December 08

## Re: Arrays - Mode/Median Problems

Posted 19 February 2010 - 09:15 PM

Can you please post your entire class, not just the methods in question? A lot of the time, the errors come from the interactions in your class, not just the individual methods. Thanks for helping us help you!

### #3 michaelgates

Reputation: 0
• Posts: 10
• Joined: 22-January 10

## Re: Arrays - Mode/Median Problems

Posted 19 February 2010 - 09:19 PM

EDIT: Alright, this is everything. I didn't change anything at all, this is exactly how it looks. I was going to attach the .jar, but I guess I can't upload .jar's, oh well.

```package testscores;

import java.util.Arrays;
import java.util.Random;
import javax.swing.JOptionPane;

public class Main {

static int[] ScoreList = new int[51];  // The array to store the test scores
static int current = 0;  // The current amount of numbers in the array

public static void main(String[] args) {
String choice;

// The text for the buttons
Object[] options = {"Enter Scores", "Display Scores", "Average", "Median", "Mode", "Standard Deviation", "High Score", "Fill With Random Numbers", "Exit"};

do { // Keeps looping until the user choses "Exit"

// The main window
choice = (String) JOptionPane.showInputDialog(null, "What would you like to do?",
"Test Scores", JOptionPane.QUESTION_MESSAGE,
null, options, null);

if (choice.equals("Enter Scores")) {
int HowMany,
Score,
count = 0;

do {  // Find out how many test scores the user would like to enter
HowMany = Integer.parseInt(JOptionPane.showInputDialog(null, "How many test scores would you like to enter right now?",
"Enter Scores", JOptionPane.QUESTION_MESSAGE));
} while (HowMany <= 0 && HowMany > 50);

for (int i = 0; i < HowMany; i++) {  // Keeps going until the amount of test scores is reached

do {
// Enter the test scores one at a time
Score = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter test score " + (i + 1) + ":", "Enter Scores", JOptionPane.QUESTION_MESSAGE));
} while (Score <= 0 && Score > 50);  // Make sure the score is valid

GetTestScores(Score);
}
} else if (choice.equals("Display Scores")) {
if (current == 0) {
JOptionPane.showMessageDialog(null, "There are no scores entered", "Display Scores", JOptionPane.ERROR_MESSAGE);
} else if (current > 0) {
JOptionPane.showMessageDialog(null, "The current entered scores are:\n" + DisplayChart(), "Display Scores", JOptionPane.PLAIN_MESSAGE);
}
} else if (choice.equals("Average")) {
if (current == 0) { //If there are no scores
JOptionPane.showMessageDialog(null, "There are no scores entered", "Average", JOptionPane.ERROR_MESSAGE);
} else if (current > 0) {
JOptionPane.showMessageDialog(null, "The average is " + ComputeAverage() + "%", "Average", JOptionPane.PLAIN_MESSAGE);
}
} else if (choice.equals("Median")) {
if (current == 0) { //If there are no scores
JOptionPane.showMessageDialog(null, "There are no scores entered", "Median", JOptionPane.ERROR_MESSAGE);
} else if (current > 0) {
JOptionPane.showMessageDialog(null, "The median is " + ComputeMedian() + "%", "Median", JOptionPane.PLAIN_MESSAGE);
}
} else if (choice.equals("Mode")) {
if (current == 0) { //If there are no scores
JOptionPane.showMessageDialog(null, "There are no scores entered", "Mode", JOptionPane.ERROR_MESSAGE);
} else if (current > 0) {
JOptionPane.showMessageDialog(null, "The mode is " + ComputeMode() + "%", "Mode", JOptionPane.PLAIN_MESSAGE);
}
} else if (choice.equals("Standard Deviation")) {
if (current == 0) { //If there are no scores
JOptionPane.showMessageDialog(null, "There are no scores entered", "Standard Deviation", JOptionPane.ERROR_MESSAGE);
} else if (current > 0) {
JOptionPane.showMessageDialog(null, "The standard deviation is " + ComputeStandardDeviation(), "Standard Deviation", JOptionPane.PLAIN_MESSAGE);
}
} else if (choice.equals("High Score")) {
if (current == 0) { //If there are no scores
JOptionPane.showMessageDialog(null, "There are no scores entered", "High Score", JOptionPane.ERROR_MESSAGE);
} else if (current > 0) {
JOptionPane.showMessageDialog(null, "The highest score is " + ComputeHighest() + "%", "High Score", JOptionPane.PLAIN_MESSAGE);
}
} else if (choice.equals("Fill With Random Numbers")) {
int amount = Integer.parseInt(JOptionPane.showInputDialog(null, "How many random numbers would you like to insert? (No more than 50)", "Random Numbers", JOptionPane.QUESTION_MESSAGE));
if (amount > 0 && amount < 50) {
Random(amount);
} else {
JOptionPane.showMessageDialog(null, "Please enter a number between zero and fifty", "Random Numbers", JOptionPane.ERROR_MESSAGE);
}
}

} while (!choice.equals("Exit")); // Keeps looping until the user choses "Exit"
// Exit
}

public static void GetTestScores(int n) {
// Put n in the next available spot in the array
ScoreList[current] = n;
current++;
}

public static String DisplayChart() {
String num = "";
for (int i = 0; i < current; i++) {
num += "       " + ScoreList[i] + "%    ";
if (i == 4 || i == 9 || i == 14 || i == 19 || i == 24 || i == 29 || i == 34 || i == 39 || i == 44 || i == 49) {
num += "\n";  // Every five numbers make a new line
}
}
return num;
}

public static double ComputeAverage() {
int total = 0;
for (int i = 0; i < current; i++) {
total += ScoreList[i];  // Add up the numbers in the array
}
return total / (current - 1);  // Divide the total by the amount of items in the array
}

public static int ComputeHighest() {
int maximum = ScoreList[0];   // First value of the array
for (int i = 1; i < ScoreList.length; i++) {
if (ScoreList[i] > maximum) { // If the number at i is bigger than the
maximum = ScoreList[i];   //   current maximum than set it to maximum
}
}
return maximum;
}

//Returns a string representation of the A's, B's, C's, D's, and F's in the class
int A = 0, B = 0, C = 0, D = 0, F = 0;
for (int i = 0; i < current; i++) {
if (ScoreList[i] >= 90) {
A++;
} else if (ScoreList[i] < 90 && ScoreList[i] >= 80) {
B++;

} else if (ScoreList[i] < 80 && ScoreList[i] >= 70) {
C++;
} else if (ScoreList[i] < 70 && ScoreList[i] >= 60) {
D++;
} else {
F++;
}
}
return (A + " A's, " + B + " B's, " + C + " C's, and " + F + " F's");
}

//This method is not working. It returns 0.0 and sets all the current items in the array to 0
public static double ComputeMedian() {
Arrays.sort(ScoreList);  //Sort the list
int middle = ScoreList.length / 2;  // middle number
if (ScoreList.length % 2 == 1) {
// There is an odd number of test scores so return the one middle number
return ScoreList[middle];
} else {
// There is an even number of test scores so return the average of the middle two
return (ScoreList[middle - 1] + ScoreList[middle]) / 2.0;
}
}

//This method returns 0 every time
public static int ComputeMode() {
// Create array of tallies, all initialized to zero
int[] tally = new int[101];
for (int i = 0; i < tally.length; i++) {
tally[i] = 0;
}

// For each array entry, increment the corresponding tally
for (int i = 0; i < ScoreList.length; i++) {
int value = ScoreList[i];
tally[value]++;
}

// Find the index of the largest tally
int maxIndex = 0;
for (int i = 1; i < tally.length; i++) {
if (tally[i] > tally[maxIndex]) {
maxIndex = i;
}
}
return maxIndex;
}

public static double ComputeStandardDeviation() {
int Q = 0;
for (int i = 0; i < ScoreList.length; i++) {
Q = Q + (ScoreList[i] - (int) ComputeAverage()) * (ScoreList[i] - (int) ComputeAverage());
}

int variance = Q / (ScoreList.length - 1);
return Math.sqrt(variance);
}

//This method does nothing. The array is unaffected after I run it...
public static void Random(int amount) {
Random gen = new Random();
for (int i = 0; i < amount + 1; i++) {
ScoreList[i] = (int)gen.nextInt(101);
}
}
}
```

This post has been edited by michaelgates: 20 February 2010 - 02:30 PM

### #4 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12267
• Posts: 45,362
• Joined: 27-December 08

## Re: Arrays - Mode/Median Problems

Posted 19 February 2010 - 09:22 PM

The main() method and constructor (if it contains any logic) are actually what I would like to see as well as the entire class definition, not just methods; as with methods, I can't get a sense for the flow of the program.

### #5 g00se

• D.I.C Lover

Reputation: 3529
• Posts: 16,004
• Joined: 20-September 08

## Re: Arrays - Mode/Median Problems

Posted 20 February 2010 - 02:16 AM

Make sure you fully randomise the entire ScoreList array or you'll find a mode of zero occurring

'amount' in Random() should therefore be the number of elements and the loop should run

```for(int i = 0;i < amount;i++)
```

This post has been edited by g00se: 20 February 2010 - 02:18 AM

### #6 michaelgates

Reputation: 0
• Posts: 10
• Joined: 22-January 10

## Re: Arrays - Mode/Median Problems

Posted 20 February 2010 - 02:25 PM

g00se, on 20 February 2010 - 08:16 AM, said:

Make sure you fully randomise the entire ScoreList array or you'll find a mode of zero occurring

'amount' in Random() should therefore be the number of elements and the loop should run

Well, I haven't ever used the random method because it doesn't work at all. It doesn't fill the list at all. So I have just been typing in some numbers when I first run. I don't think it would matter anyway, because its just a big array and I always use ScoreList.length...

### #7 g00se

• D.I.C Lover

Reputation: 3529
• Posts: 16,004
• Joined: 20-September 08

## Re: Arrays - Mode/Median Problems

Posted 20 February 2010 - 03:38 PM

Quote

Well, I haven't ever used the random method because it doesn't work at all. It doesn't fill the list at all.

Please show how you're calling it

Quote

I don't think it would matter anyway, because its just a big array and I always use ScoreList.length...

Using the whole length won't help you if the array has not been fully filled with random values - since the default value will be zero (which you'll therefore get as the mode)

This post has been edited by g00se: 20 February 2010 - 03:39 PM