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:
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.
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.
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.