Sorting the marks and the Students names together...

How to sort them together. I want to sort students marks in ascending order but also would like to sort the names which is array of strings in my program.
Please help. So the names should be saorted when the marks are sorted and they are two different arrays.
Help!
I got my function as:

1
2
3
4
5
6
7
8
9
10
11
12
13
void sorter (int arrayname[] , string names[], int lengtharray)
{
    sort (arrayname, arrayname +lengtharray);
    
    
    for (int m=0; m < lengtharray ; m++)
    {
        cout << names[m] << " :  " << arrayname[m] << "," ;    
    
    }
    
    cout << "Sorted" << endl;
}



well I cannot se classes. no.
I have inputting names and marks in arrays
like this
1
2
3
4
5
for ( int m=0; m < totalnumberofstudents; m ++)
{
cin >> names[m];
cin >> marks[m];
Last edited on
Do they have to be arrays?
Can you give an example how those two arrays would look? Cause I don't understand how you identify what marks belong to a student. Or do they all have the same number of marks, so the first 3 marks would be those of the first student, the next 3 those of the second student and so on?

You could create a class for students and their grades, something like:

1
2
3
4
5
6
7
8
9
10
11
12
class student {
    int grades[5];
    string name;
      
    public:
       void set_grades (int a[5]) {
       grades = a;
       }
       void set_name (string x) {
          name = x;
       }
}


And then you could do something like:

student Student_list[];

to get an array of students. That way you would have students and their grades together and you could sort the objects of type student by name. The array grades, containing the grades of a student, could then be sorted independently for each object of type student.

Sorry this is not very structured, if you have any questions feel free to ask.

edited :p

They have to be in arrays. I cannot change because I will have to change my whole program
bump
closed account (D80DSL3A)
If you must keep the data in 2 separate arrays and cannot use a structure then I think you will have to write your own sort function, so you can swap the elements in both arrays as the sort is being done.
could you give some examples?
like name = "ttt", "rrr"
index = 1, 2
what kind of results do you want?
Maybe it could be solve by functor
just sort the marks
eg:
a: 30
b: 90
c: 45

when 30 45 and 90 are sorted using sort i want the a b and c to be sorted as well.

up
basic sort function looks like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int sort(int marks[])
{	int temp;
	int x;
	int swaps;
	while(swaps>0){
	for(x=0;marks[x];x++)// need to add somethin like x<(number of entries in marks[])
		                              // delete marks[x] from for statement entirely
		if (marks[x] < marks[x+1])// this determines which direction to go
			swaps=0;  //counts if a sort was made or not
			temp=marks[x+1];
	marks[x+1]=marks[x];
	marks[x]=temp;
		swaps++;
}

}


Im pretty sure this should work I got to double check it against this book i'm reading.
Last edited on
ok thank you i will try to imply it
Topic archived. No new replies allowed.