Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Help with arrays. Computing averages, sorting the array,

1. ## Help with arrays. Computing averages, sorting the array,

//I need to get an array of scores from the user in an applet, then with the array I need to compute the average, count how many scores were above and below the average, sort the array in ascending order (without arrays.sort) and display it all on the applet.

It's fine that they are all integers instead of floats or doubles.

The average keeps coming out weird. I'll put a number in ten times and then it will give me an average lower than that number. And the below average always shows it gets more than it should.

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class Practice extends Applet implements ActionListener {

Label prompt1;
TextField input1;
Label prompt2;
TextField input2;
boolean done;
int number;
int computeAverage;
int aboveAverage;
int belowAverage;
int next;
int[] score = new int[10];

//A bubble sort I found on the internet, I don't know how to implement it.

for(int x = 0; x < score.length; x++)

for (int y = 0; y < score.length - 1; y++){
int tempa = score [y];
int tempb = score [y+1];
if(score[y] > score[y+1]){
score[y]=tempb;
score[y+1] = tempa;
}
//My method that gives an average lower than it should be.

int computeAverage(int score[], int length) {
int total = 0;
for (int counter = 0; counter < score.length; counter++)
total += score[counter];
int average = total / score.length;
return average;

}
//This is supposed to count how many score are above the average, but it always gives one to the next method belowAverage even if it shouldn't.

int aboveAverage(int score[], int length) {
int average = (computeAverage(score, length));
int total = 0;
for (int counter = 0; counter < score.length; counter++) {
if (score[counter] >= average) {
total++;
}
}
{
}
}

//This one always says that there is one below average even if there isn't.

int belowAverage(int score[], int length) {
int average = (computeAverage(score, length));
int total = 0;
for (int counter = 0; counter < score.length; counter++) {
if (score[counter] < average) {
total++;
}
}
{
}
}

//Normal working textfield and prompt.

public void init() {
prompt1 = new Label("Enter a Score");
input1 = new TextField(10);
}

//I need to get my sorted array printed here somehow.

public void paint(Graphics g) {
g.drawString("Enter -1 to quit early", 10, 50);

if (done) {
g.drawString(
"The class average is "
+ (computeAverage(score, score.length)), 10, 90);
g.drawString(
"The # of scores greater than or equal to the average is "
+ aboveAverage(score, score.length), 10, 110);
g.drawString("The # of scores less than the average is "
+ belowAverage(score, score.length), 10, 130);
}
}

public void actionPerformed(ActionEvent e) {
number = Integer.parseInt(input1.getText());
if (number > 100) {
showStatus("Scores over 100 are invalid");
}

if ((number < 0) || (next >= 9)) {
done = true;
input1.setEditable(false);
showStatus("No more scores can be entered");
} else {
score[next++] = number;
}

repaint();

}
}

2. ## Re: Help with arrays. Computing averages, sorting the array,

When posting code, please use the highlight tag to preserve formatting.

Your first step is to figure out which part of this fails. Step through this with a debugger, or at least add some print statements, to figure that out. You have to test each part of your program in isolation instead of giving us a whole big mess and saying that it doesn't work.

Why not use Arrays.sort() instead of trying to implement an algorithm you aren't even sure works?