If you can use modern c++ tools like std::vector and other cool business like std::sort (which sorts an std::vector), it'd be much cleaner than doing it yourself with raw arrays. Learning to use container classes and the built-in algorithms of the language will serve you well.
#include <vector>
#include <algorithm>
#include <iostream>
struct Student //structure to hold student ID and grade info
{
Student(int id, int grade):id(id),grade(grade){}
int id;
int grade;
};
//comparison operator to use with std::sort
bool operator <(const Student& lhs, const Student& rhs)
{
return lhs.grade < rhs.grade;
}
int main()
{
//You are starting with raw arrays...
int studentID[5]{1,2,3,4,5};
int grade[5]{90,91,73,62,87};
//convert to std::vector of Student objects
//not safe indexing into raw array, just an example
std::vector<Student> students;
for(unsigned int i=0;i<5;++i){
students.push_back(Student(studentID[i],grade[i]));}
//sort the vector (using the less-than operator provided above)
std::sort(students.begin(),students.end());
//reverse the vector, making it high-to-low order
std::reverse(students.begin(),students.end());
//print the result (uses std::for_each with a lambda function)
std::for_each(students.begin(),students.end(),
[](Student s){std::cout<<"ID: "<<s.id<<" grade: "<<s.grade<<std::endl;});
}
Output:
ID: 2 grade: 91
ID: 1 grade: 90
ID: 5 grade: 87
ID: 3 grade: 73
ID: 4 grade: 62