How to sort an array of integers and an array of strings at the same time?!?

I need to write a program that allows the user input 5 student's names and grades. Then the program would sort the data and output who has the first highest grade, then second, third, and so on.
I used bubble sort to sort the grades, but i have no idea of how to sort the names. Is there any way to sort both integers and strings at the same time? Or is there an easier way to write the program? Here is what i got so far...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
using namespace std;

int main()
{
	int grade[5];
	char name[5][10];
	int count;
	int t;
	for (count = 0; count < 5; count++)
	{
        cout << "Please enter the first name of student " << (count + 1) << endl;
		cin >> name[count];

		cout << "Please enter the score of runner " << (count + 1) << endl;
		cin >> grade[count];

	}
	
	for(int j =0; j<4; j++)
	{
            for(int k=j+1; k<5; k++)
            {
                    if (grade[j] < grade[k])
                    {
                                t=grade[j];
                                grade[j]=grade[k];
                                grade[k]=t;
                    }
            }
    }
         
    return 0;
}


Please help~
Thanks~~
Last edited on
You should make a struct:

1
2
3
4
5
6
7
8
9
struct Student {
   int    grade;
   char name[10];
};

int main() {
   Student students[5];
   // etc.
}


Alternatively, your bubble sort needs to perform the exact same swaps on the name array as it is doing on the grade array.
I haven't learn anything about struct yet, so i really don't know how it works. =(
How should i code it so the name array would perform the same swaps?
The name has to change at the same time the grade does for example

name[i][j];
grade[i];

But a better choice would be to learn about structs and use one, theres nothing stopping you and there pretty simple, a struct just holds a bunch of variables under a single name.

Just a way of grouping things together.
Topic archived. No new replies allowed.