// Sorting Grades
// Programmer: Bob Comer
// Completed : 4/27/99
// Status    : Complete
//
// Description:
//   This program reads a list of grades for a class and prints
//   prints them back out sorted from largest to smallest. The
//   program will handle up to CLASS_SIZE grades.
//
//*************************************************************

#include <iostream.h>
#include <iomanip.h>

const int CLASS_SIZE = 20;   // Number of students in class

void GetGrades( int grades[], int &numGrades );
void Sort( int list[], int length );
void PrintGrades( int grades[], int numGrades );

int main()
{
   int grades[CLASS_SIZE];   // List of grades for a class
   int numGrades;            // Number of grades stored

// Get the list of grades
   GetGrades( grades, numGrades );

// Sort the grades from largest to smallest
   Sort( grades, numGrades );

// Print the sorted list of grades
   PrintGrades( grades, numGrades );

// Signal normal exit
   return 0;
}

//************************************************************
// Function: GetGrades
//
// This function inputs a list of student grades.
//************************************************************

void GetGrades( int grades[], int &numGrades )
{
   int studNo;    // Student number
   int grade;     // A student grade

// Get list of student grades
   studNo = 0;
   cout << "Enter grade for student " << studNo + 1
        << " (or -1 to quit): ";
   cin >> grade;
   while (grade != -1 && studNo < CLASS_SIZE)
   {
      grades[studNo] = grade;
      studNo++;
      cout << "Enter grade for student " << studNo + 1
	        << " (or -1 to quit): ";
      cin >> grade;
   }
   numGrades = studNo;
}

//************************************************************
// Function: Sort
//
// This function sorts a list of integers from largest to
// smallest.
//************************************************************

void Sort( int list[], int length )
{
   int index;     // position of item in list
   bool sorted;   // flag to test if list is sorted
   int temp;      // temporary variable to use in sorting

   sorted = false;
   while ( !sorted )
   {
      sorted = true;
      for ( index = 0; index < length - 1; index++ )
      {
	      if (list[index] < list[index+1])
	      {
	         temp = list[index];
	         list[index] = list[index+1];
	         list[index+1] = temp;
            sorted = false;
         }
      }
   }
}

//************************************************************
// Function: PrintGrades
//
// This function prints each grade in the list.
//************************************************************

void PrintGrades( int grades[], int numGrades )
{
   int studNo;    // Student number

// Set up format for floating point output
   cout << setiosflags(ios::fixed) << setprecision(2);

// Print heading
   cout << setw(10) << "Student No"
	     << setw(8)  << "Grade"
	     << endl;

   for (studNo = 0; studNo < numGrades; studNo++)
      cout << setw(10) << studNo + 1
	        << setw(8)  << grades[studNo]
	        << endl;
}

