How do you concatenate two arrays?

I'm in a basic programming class and everything is done in pseudocode.

My question is, how do you concatenate two arrays?

I have a one-dimensional array that lists the names of the students, and I have a two-dimensional array that lists the top eight points of each student ... it's all fine and dandy, but now I need to sort the arrays by student name. I poked myself on the internet and read the book chapter twice, it only briefly mentions joining two arrays, but doesn't show examples.

If that helps, we're using bubble sort, which is something I'm pretty familiar with ... I can sort the names, which is the easy part, but I don't know how to sort the scores so they don't go out of order.

Thanks for your input!

Sidenote: I got it! I ended up saying Greg Huglill. As I added in my comment on his proposal, I started to randomly throw lines of code until this idea hit me ... it doesn't look very pretty (one module swapped names, another swapped evaluations, and a third even then to swap by separate student grades earlier in a multidimensional array), but it did work ... in no way test it in the language since I don't have a compiler and I don't have enough knowledge to make the pseudocode into actual code if I had to load it. but on the paper I typed it on it sounds very good!

As I mentioned in the post, I thank everyone for their early and helpful understanding, I did not even think that I would receive an answer today, thank you all for your help!

Jeffrey

+1


source to share


4 answers


What you can do is this: when you are sorting names and you need to swap two positions, do the same swap in the points array. This way, any changes you make to the array of names will be reflected in the array of points. When you're done, the scores will be in the same order as the names.



There are more efficient ways to do this with different data structures, as other comments show.

+1


source


Define a simple class Student 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
}

      

it's even easier



    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 job for you.

+2


source


Your premise is wrong. First of all, you don't need to have two arrays.

You must have one array of objects, each containing the student's name and grade:

public class Record
{
    public string Student;
    public int[] Scores;
} 

      

0


source


Two approaches: first, when sorting names, every time you swap two names, swap rows (or columns or whatever you want to call them) at the same positions. Finally, the grades should still be in sync with the names.

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

0


source







All Articles