Created
March 1, 2024 17:13
-
-
Save muromtsev/7aad6d2b5290520b765fdedd24fd7b4c to your computer and use it in GitHub Desktop.
Sorting students by grades
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
struct Students | |
{ | |
std::string name; | |
int grade; | |
}; | |
void sortNames(Students *students, int length) | |
{ | |
for(int start = 0; start < length; start++) | |
{ | |
int max = start; | |
for(int current = start + 1; current < length; current++) | |
{ | |
if(students[current].grade > students[max].grade) | |
max = current; | |
} | |
std::swap(students[start], students[max]); | |
} | |
} | |
int main() | |
{ | |
int countStudents; | |
do | |
{ | |
std::cout << "How many students do you want to enter? "; | |
std::cin >> countStudents; | |
} while(countStudents <= 1); | |
Students *students = new Students[countStudents]; | |
for(int idx = 0; idx < countStudents; ++idx) | |
{ | |
std::cout << "Enter name #" << idx + 1 << ": "; | |
std::cin >> students[idx].name; | |
std::cout << "Enter grade #" << idx + 1 << ": "; | |
std::cin >> students[idx].grade; | |
} | |
sortNames(students, countStudents); | |
for(int i = 0; i < countStudents; i++) | |
std::cout << students[i].name << " got a grade of " << students[i].grade << "\n"; | |
delete[] students; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment