views:

465

answers:

4

I'm in a basic programming class, and everything is done in pseudo code.

My question is this: How do you link two arrays?

I have a single-dimensional array that lists students names, and I have a two-dimensional array that lists the top eight scores of each student...this is all fine and dandy, but now I need to sort the arrays by the students name. I'm poked around online and read through the books chapter twice, it only briefly mentions linking two arrays but shows no examples.

If it's any help, we are using bubble-sorting, and that is what I am fairly familiar with...I can sort the names, that's the easy part, but I don't know how to sort the grades so they do not go out of order.

Thanks for the input!

Sidenote: I got it figured out! I ended up doing how Greg Hewgill had mentioned. As I put in my comment to his suggestion, I started randomly throwing in lines of code until that idea hit me...it doesn't look pretty (one module swapped the names, another to swap the grades, and a third even then to swap the individual students grades earlier on in a multidimensional array), but it indeed seemed to work...no way to test it in a language as I have no compiler nor have I enough knowledge to make the pseudo code into actual code if I were to download one, but it sounds really good on the paper I typed it out on!

As I also mentioned in the note, I do thank everyone for their speedy and helpful insight, I actually didn't even think I'd get a reply tonight, thank you everyone again for all your help!

Jeffrey

A: 

Your premise is wrong. You shouldn't have two array in the first place.

You should have one array of objects, each of which holds a student's name and his scores:

public class Record
{
    public string Student;
    public int[] Scores;
}
James Curran
Tell that to the teacher (if only you could), not the hapless student.
ysth
This is ironic... *grins*
Tomalak
As I said, it's a very basic class, I haven't taken a programming language since highschool (nearly six years ago now), and at the time it was c++, the teacher seems to be teaching the utmost of basics, which is great...but I was always horrible at making two things work together, multitasking sucks
Jeff
You don't know if the programming class is already at the Class level. Furthermore you don't know the language, maybe there aren't classes, say in a functional language.
boutta
@boutta: The language is pseudocode, apparently
Artelius
+1  A: 

What you may want to do is the following: As you're sorting the names and you have to swap two positions, do the same swap in the array of scores. That way, all changes that you make to the names array will be reflected in the scores array. When you're done, the scores will be in the same sorted order as the names are.

There are more effective ways of doing this with different data structures, as other comments will show.

Greg Hewgill
I started doing as I always do, that is, writing random lines of code till I found what worked, and I actually ended up doing it this way, thank you everyone for all your immediate advice and input! I know where I'll be turning to from now on for assistance. ^-^
Jeff
Ah, so, that's how that works, thanks ^-^;
Jeff
Not registered, so cannot up vote, if I'm lucky I can get 6 hours of sleep if I go to bed now before I have to be at this class...sleep...register...I'll register tomorrow ^-^;
Jeff
A: 

Two approaches: first, when sorting the names, each time you exchange two names, exchange the rows (or columns or whatever you want to call them) of scores in the same positions. At the end, the scores should still be in sync with the names.

Second, instead of sorting the names, create a third array that will contain the indexes into either of the other two arrays, initially 0 through n-1, but then sorted, comparing name[a] and name[b], instead of sorting the names array itself.

ysth
+2  A: 

Define a simple Student class like this:

public class Student : IComparable<Student>
{
    public string Name { get; set; }
    public int[] Scores { get; set; }

    #region IComparable<Student> Members

    public int CompareTo(Student other)
    {
        // Assume Name cannot be null
        return this.Name.CompareTo(other.Name);
    }

    #endregion
}

then even simpler

    var students = new[] {
        new Student(){ Name = "B", Scores = new [] { 1,2,3 } },
        new Student(){ Name = "C", Scores = new [] { 3,4,5 } },
        new Student(){ Name = "A", Scores = new [] { 5,6,7 } }
    };

    Array.Sort(students);

will do the work for you.

Petar Petrov