The never ending arrays...
If you can’t use a vector in C++...
First, use a struct:
1 2 3 4 5
|
struct Student
{
int ID;
int grade;
};
|
Make an array (or vector!) of students:
1 2 3 4 5 6 7 8 9
|
int main()
{
constexpr int MaxStudents = 100;
Student students[ MaxStudents ];
int NumStudents = 0;
--or--
std::vector <Student> students;
|
Write a function to get a single student from the user. Since this is C++, write it as a stream extraction operator:
1 2 3 4
|
std::istream& operator >> ( std::stream& ins, Student& student )
{
return ins >> student.ID >> student.grade;
}
|
Now you can ask for any number of students, one line at a time:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
std::cout << "Please enter 1 to " << MaxStudents << " students, one line at a time, as \"ID GRADE\".\n"
"Press Enter twice to finish.\n";
std::string s;
while (getline( std::cin, s ) && !s.empty())
{
// Convert the string to a Student
std::istringstream ss{ s };
Student student;
ss >> student;
// Add the student to our array
students[ NumStudents++ ] = student;
if (NumStudents == MaxStudents) break;
--or--
// Add the student to our vector
students.push_back( student );
if (students.size() == MaxStudents) break;
}
std::cout << "Thank you.\n";
|
Now you have a friendly list of students.
The next step is to sort it. In order to sort something, you have to be able to tell whether one student is less than another. Write a function. (And, again, since this is C++, you might as well overload the less-than operator.)
1 2 3 4
|
bool operator < ( const Student& a, const Student& b )
{
return a.grade < b.grade;
}
|
You can write other comparisons as well, but this is all you need.
In my opinion, the easiest way to sort things is using a
selection sort. You use it naturally in real life. For example, how would you sort the following list?
8 5 2 6 9 3 1 4 7
I would look through the list and find the smallest number, then write it down.
8 5 2 6 9 3 1 4 7
1
Then I would look through and find the next smallest number, and write it down.
8 5 2 6 9 3 1 4 7
1 2
I would continue until there are no more numbers to select from the unsorted list.
8 5 2 6 9 3 1 4 7
1 2 3 4 5 6 7 8
8 5 2 6 9 3 1 4 7
1 2 3 4 5 6 7 8 9
In a computer, we do it pretty much the same way, but we don’t have to create a new list. Instead of crossing out, we
swap. See
http://www.cplusplus.com/faq/sequences/sequencing/sort-algorithms/selection-sort/ for a pretty, animated example.
That means we only need two loops:
• An outer loop which tracks the number of sorted items.
• An inner loop to find the smallest item in the unsorted part.
Write yourself a function that sorts the list.
1 2 3 4 5
|
void sort_students( Student students[], int NumStudents );
--or--
void sort_students( std::vector <Student> & students );
|
You can also write a function to swap two students:
1 2 3 4 5 6 7
|
void swap( Student& a, Student& b )
{
Student
c = a;
a = b;
b = c;
}
|
Life is easy. I tried to write a basic structure a couple of times now, but it really cannot be done without just giving you code. Do your best.
Displaying the output is just a matter of looping over the students and printing them.
1 2 3 4 5 6 7 8 9
|
std::cout << std::setw( 10 ) << "STUDENT" << " GRADE\n";
for (int n = 0; n < NumStudents; n++)
std::cout << std::setw( 10 ) << students[ n ].ID << " " << students[ n ].grade << "\n";
--or--
std::cout << std::setw( 10 ) << "STUDENT" << " GRADE\n";
for (auto student : students)
std::cout << std::setw( 10 ) << student.ID << " " << student.grade << "\n";
|
Good luck!