C++ for statement loop to read student grade and calculate average

Description

C++ for statement loop to read student grade and calculate average

#include <iostream>
using namespace std;
int main()/*  w  w  w  .  j  a  v  a 2 s  . com*/
{
   const int NUMGRADES = 4;
   const int NUMSTUDENTS = 20;
   int i, j;
   double grade, total, average;
   for (i = 1; i <= NUMSTUDENTS; i++)  // start of outer loop
   {
      total = 0;                      // clear total for this student
      for (j = 1; j <= NUMGRADES; j++)  // start of inner loop
      {
         cout << "Enter an examination grade for this student: ";
         cin  >> grade;
         total = total + grade;         // add the grade to the total
      }                               // end of inner for loop
      average = total / NUMGRADES;      // calculate the average
      cout << "\nThe average for student " << i
      << " is " << average << "\n\n";
   }                                 // end of outer for loop
   return 0;
}



PreviousNext

Related