// Lab 8 - Grade array processing
//
// 1. Modify the PrintDiff() function in the following program
//    to process a single element in the array instead of the
//    whole array. Then modify the main() function so that your
//    new version of the program does the same thing as the
//    original program.
//
// Grade Program
// Programmer : Bob Comer
// Modified by: your name here
// Completed  : 4/18/99
// Status     : Complete
//
// Description:
//   This program reads a grade for each person in a class and
//   calculates how far the grade is from the average. This
//   program will process exactly CLASS_SIZE grades.
//
//*************************************************************

#include <iostream.h>
#include <iomanip.h>

const int CLASS_SIZE = 20;   // Number of students in class

void GetGrades( int grades[] );
float Avg( int grades[] );
void PrintDiff( int grades[], float average);

int main()
{
   int grades[CLASS_SIZE];      // List of grades for a class
   float average;       // Average grade

// Get the list of grades
   GetGrades( grades );

// Calculate average grade
   average = Avg( grades );

// Print difference between each grade and the average
   PrintDiff( grades, average );

// Signal normal exit
   return 0;
}

//************************************************************
// Function: GetGrades
//
// This function inputs a list of student grades.
//************************************************************

void GetGrades( int grades[] )
{
   int studNo;    // Student number

// Get list of student grades
   studNo = 0;
   while (studNo < CLASS_SIZE)
   {
      cout << "Enter grade for student " << studNo + 1 << ": ";
      cin >> grades[studNo];
      studNo++;
   }
   return;
}

//************************************************************
// Function: Avg
//
// This function calculates the average of the grades.
//************************************************************

float Avg( int grades[] )
{
   int studNo;    // Student number
   int sum;       // Sum of the grades

// Sum the student grades
   sum = 0;
   studNo = 0;
   while (studNo < CLASS_SIZE)
   {
      sum = sum + grades[studNo];
      studNo++;
   }

// Return average of grades
   return float(sum) / float(CLASS_SIZE);
}

//************************************************************
// Function: PrintDiff
//
// This function prints each grade and it's difference
// from the average.
//************************************************************

void PrintDiff( int grades[], float average)
{
   int studNo;    // Student number

// Set up format for floating point output
   cout << setiosflags(ios::fixed) << setprecision(2);

// Print class average
   cout << "\nThe class average is : " << average
	<< endl << endl;

// Print heading
   cout << setw(10) << "Student No"
	<< setw(8)  << "Grade"
	<< setw(16) <<"Dist from avg"
	<< endl;

   studNo = 0;
   while (studNo < CLASS_SIZE)
   {
      cout << setw(10) << studNo + 1
	   << setw(8)  << grades[studNo]
	   << setw(16) << grades[studNo] - average
	   << endl;
      studNo++;
   }
   return;
}

