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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
#include <iostream>
#include <string>
#include <utility>
#include "keep_window_open.h"
struct Student
{
std::string name;
int grade;
};
int takeSizeInput();
void fillArray(Student students[], const int size);
void selectionSortNames(Student students[], const int size);
void displayArray(const Student students[], const int size);
int main()
{
using namespace std;
cout << "How many students' names would you like to enter? ";
int size = takeSizeInput();
Student *students = new Student[size];
fillArray(students, size);
selectionSortNames(students, size);
displayArray(students, size);
delete[] students;
keep_window_open();
return 0;
}
int takeSizeInput()
{
using namespace std;
int size;
cin >> size;
cin.ignore(32767, '\n');
return size;
}
void fillArray(Student students[], const int size)
{
using namespace std;
for (int index = 0; index < size; ++index)
{
cout << "Enter name #" << index + 1 << ": ";
cin >> students[size].name;
cin.ignore(32767, '\n');
cout << "Enter grade #" << index + 1 << ": ";
cin >> students[size].grade;
cin.ignore(32767, '\n');
}
}
void selectionSortNames(Student students[], const int size)
{
using namespace std;
for (int startIndex = 0; startIndex < size; ++startIndex)
{
int largestIndex = startIndex;
for (int currentIndex = startIndex + 1; currentIndex < size; ++currentIndex)
{
if (students[currentIndex].grade > students[largestIndex].grade)
{
largestIndex = currentIndex;
}
}
std::swap(students[startIndex].grade, students[largestIndex].grade);
}
}
void displayArray(const Student students[], const int size)
{
using namespace std;
for (int index = 0; index < size; ++index)
{
cout << students[index].name << "\n";
cout << students[index].grade << "\n";
}
cout << "\n";
}
|