Java Programmers, I Need Help!
| bingo |
|
|
|
Anyone know java? I need some help with a Bubble Sort. |
| nick |
|
|
|
I know how to do a bubble sort in just about any other language, except Java Luckily I do know how to use Google : http://leepoint.net/notes-java/data/arrays/32arraybubblesort.html |
| bingo |
|
|
|
i know i've used that but there is another problem with it that I can't find the solution to anywhere. |
| nick |
|
|
|
What is the problem, a bubble sort is pretty simple. |
| bingo |
|
|
|
Its quite hard to explain but I've been given a series of student names and marks. I have to calculate the average of level 1 and level 2 marks and then sort them and output the grade with the student name. I can calculate the average and output it so that the marks are sorted in asscending order however the names just output Joe Bloggs 1 38% import java.util.*;
public class weighted_tutorial_4
{
public static final int MAX_STUDENTS = 20;
public static String[] surname = { "Bloggs1", "Bloggs2", "Bloggs3", "Bloggs4", "Bloggs5", "Bloggs6", "Bloggs7", "Bloggs8", "Bloggs9", "Bloggs10", "Bloggs11", "Bloggs12" };
public static String[] firstname = { "Joe1", "Joe2", "Joe3", "Joe4", "Joe5", "Joe6", "Joe7", "Joe8", "Joe9", "Joe10", "Joe11", "Joe12" };
public static int[] person_id = { 123401, 123402, 123403, 123404, 123405, 123406, 123407, 123408, 123409, 123410, 123411, 123412 };
public static double[] lev2_mean = { 43.56, 45.23, 78.65, 50.00, 60.00, 80.23, 45.23, 45.23, 50.01, 49.99, 99.99, 40.00 };
public static double[] lev3_mean = { 40.00, 55.33, 67.54, 65.00, 62.63, 70.56, 54.23, 36.45, 55.54, 59.34, 90.87, 40.00 };
public static double[] overall_mean = new double[MAX_STUDENTS];
public static int no_of_students = 12;
public static void sort_students()
{
double temp1;
int temp2;
int n = no_of_students;
//String[] firstname;
//String[] surname;
int person_id;
for (int i=0; i < n; i++)
{
// Sort the numbers if there is more than one
for (int j=0; j overall_mean[k])
{
// Swap hem
temp1 = overall_mean[j];
overall_mean[j] = overall_mean[k];
overall_mean[k] = temp1;
}
}
}
}
}
public static void main(String[] args)
{
// Declare and initialise variables
int i;
// Work out overall mean for each student
for (i=0; i < no_of_students; i++)
{
overall_mean[i] = ((lev2_mean[i] * 0.25) + (lev3_mean[i] * 0.75));
}
// Sort student arrays
sort_students();
// Output results
for (i=0; i < no_of_students; i++)
{
// Output id, name and overall mean
//System.out.println(person_id[i] + " " + firstname[i] + " " + surname[i] + " : " + overall_mean[i]);
// Output degree classification
if(overall_mean[i] > 69.99)
System.out.println(person_id[i] + " " + firstname[i] + " " + surname[i] + " : " + overall_mean[i] + " 1st");
else
if(overall_mean[i] > 59.99)
System.out.println(person_id[i] + " " + firstname[i] + " " + surname[i] + " : " + overall_mean[i] + " 2:1");
else
if(overall_mean[i] > 49.99)
System.out.println(person_id[i] + " " + firstname[i] + " " + surname[i] + " : " + overall_mean[i] + " 2:2");
else
if(overall_mean[i] > 39.99)
System.out.println(person_id[i] + " " + firstname[i] + " " + surname[i] + " : " + overall_mean[i] + " 3rd");
else
if(overall_mean[i] < 39.99)
System.out.println(person_id[i] + " " + firstname[i] + " " + surname[i] + " : " + overall_mean[i] + " Fail");
//System.out.println(person_id[i] + " " + firstname[i] + " " + surname[i] + " : " + overall_mean[i]);
}
}
}
|
| nick |
|
|
|
You're swapping the overall_mean elements to sort them, but not swapping the corresponding firstname and surname elements, you your names stay in the order they were put in the array but the overall_means are sorted and become dissconected from the correct name |
| bingo |
|
|
|
that's how i was mean't to put it but i don't know how to "connect" the names to the marks. |
| nick |
|
|
|
I guess : // Swap hem temp_string = firstname[j]; temp_string = surname[j]; But that looks very messy, I assume there is some way to make a record (kind of) from the score, fistname and last name, then sort the records using the score as the key ? |
| mr.white |
|
|
|
nick, you keep making that typo |
| eliot_a |
|
|
|
its hot on his brain |
| nick |
|
|
|
nick, you keep making that typo
|









