Lab Assignment

We must create a program that will print a sorted list of all the students and the corresponding scores listing the highest score first and the lowest score last. In this case I would need a Bubble Sort.

Question

This is my "Bubble Sort" method shown in the code block below.

public static void bubbleSort(String [] name, double [] score)
    {
        double temp;
        int counter;
        int index = 0;
        for(counter = 0; counter < score.length - 1 - counter; index++)
        {
            if(score[index] > score[index + 1])
            {
                temp = score[index];
                score[index] = score[index + 1];
                score[index + 1] = temp;
            }
        }
    }

Looking at the way I have written this code I did not use the String [] name array in the method so the names will not be sorted along side the corresponding exam score.

How would i fix this code to sort not only the scores but print the names that go along side the scores? Do I need another method to accpet the names?

That is my question. How would I re-write this code so the name will be moved at the same time as the exam score is moved? Sorry if this question is simple, the logic of this is getting slightly confusing.

Besides, the bubble sort code that I wrote is only sorting the exam score and will not sort the score with the name.

It's an old bit of lore. You should try it.

Added with edit. You have the index. Now use that to move name just like you did so far with the score.

Okay. I just want to make sure I understand what you are saying.

You are saying that as I swap the score in the "if statement", swap the name at the same time. If so, then can you tell me if this is a possible soution (I am having trouble with other parts of the program so I can't run it to see if this line works but I know evenutally I would have to run this code for myself):

public static void bubbleSort(String [] name, double [] score)
    {
        double temp;
        int counter;
        int index = 0;
        String temp2 = " ";

        for(counter = 0; counter < score.length - 1 - counter; index++)
        {
            if(score[index] > score[index + 1])
            {
                temp = score[index];
                score[index] = score[index + 1];
                score[index + 1] = temp;

                temp2 = name[index];
                name[index] = name[index + 1];
                name[index + 1] = temp2;
            }
        }
    }
commented: That's the idea. That way name follows it's score by index. +0

Thank You. I will try that and see if it works.

Hey tara_2, your code is ok?

I think that is necessary a "while" before "for".
You need to check the list (score) one more time.

Thank you for the added help. I actually changed to "dynamic arrays" since we learned it in class and turned the assignment into the teacher recently. However, you all really helped me and I hope to seek assistants from forms like this in the future. Also, the teacher recommend that I use a "While Loop" also so, your right about that. Thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.