Young Fashion Entrepreneur of the year 2009Forgotten PeacockMy Sugarland shop openingWIN AN EMPORIO ARMANI WATCH UP TO £150Golden Shears 2009Clemens en August on tour in the UK
FASHION, FROM THE CONCRETE TO THE CATWALK. JOIN IN.

 

Java Programmers, I Need Help!

bingo Posted: Wed, 28/03/2007 - 10:41am
bingo's picture

Posts: 88
Joined: 2003-06-23

Anyone know java? I need some help with a Bubble Sort.

nick Posted: Wed, 28/03/2007 - 11:13am
nick's picture

Posts: 18765
Joined: 2003-05-02

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 Posted: Wed, 28/03/2007 - 11:17am
bingo's picture

Posts: 88
Joined: 2003-06-23

i know i've used that but there is another problem with it that I can't find the solution to anywhere.

nick Posted: Wed, 28/03/2007 - 11:23am
nick's picture

Posts: 18765
Joined: 2003-05-02

What is the problem, a bubble sort is pretty simple.

bingo Posted: Wed, 28/03/2007 - 11:30am
bingo's picture

Posts: 88
Joined: 2003-06-23

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%
Joe Bloggs 2 43%
etc
i.e the lowest score is always output next to joe bloggs 1 etc if that makes any sense.

 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 Posted: Wed, 28/03/2007 - 11:42am
nick's picture

Posts: 18765
Joined: 2003-05-02

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 Posted: Wed, 28/03/2007 - 11:49am
bingo's picture

Posts: 88
Joined: 2003-06-23

that's how i was mean't to put it but i don't know how to "connect" the names to the marks.

nick Posted: Wed, 28/03/2007 - 11:53am
nick's picture

Posts: 18765
Joined: 2003-05-02

I guess :

// Swap hem
temp1 = overall_mean[j];
overall_mean[j] = overall_mean[k];
overall_mean[k] = temp1;

temp_string = firstname[j];
firstname[j] = firstname[k];
firstname[k] = temp_string;

temp_string = surname[j];
surname[j] = surname[k];
surname[k] = temp_string;
}

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 Posted: Wed, 28/03/2007 - 6:05pm
mr.white's picture

Posts: 11818
Joined: 2004-07-10

nick, you keep making that typo

eliot_a Posted: Wed, 28/03/2007 - 10:48pm

Posts: 1722
Joined: 2006-01-31

its hot on his brain

nick Posted: Mon, 02/04/2007 - 11:33am
nick's picture

Posts: 18765
Joined: 2003-05-02

mr.white wrote:

nick, you keep making that typo

Laughing out loud I don't even notice it