Use nested for loop to read student grade and calculate average - C++ Statement

C++ examples for Statement:for

Description

Use nested for loop to read student grade and calculate average

Demo Code

#include <iostream>
using namespace std;
int main()/*from   w w  w .  j ava 2  s.  c o  m*/
{
   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;
}

Result


Related Tutorials