C++ const define array size

Description

C++ const define array size

#include <iostream>
using namespace std;
#include <iomanip.h>
void pr_scores(float scores[]);
const int CLASS_NUM = 6;  // Constant holds array size.
void main()/* w  w  w. j  av a 2 s . com*/
{
   char s_name[] = "University";
   float scores[CLASS_NUM] = {8.7, 9.4, 6.0, 7.0,10.0, 6.7};
   float average=0.0;
   int ctr;

   pr_scores(scores);

   for (ctr=0; ctr<CLASS_NUM; ctr++)
   {
      average += scores[ctr];
   }
   // Computes the average.
   average /= float(CLASS_NUM);
   cout << "At " << s_name << ", your class average is " << setprecision(2) << average;
   return;
}
void pr_scores(float scores[CLASS_NUM])
{
   // Prints the six scores.
   int ctr;
   cout << "Here are your scores:\n";         // Title
   for (ctr=0; ctr<CLASS_NUM; ctr++)
      cout << setprecision(2) << scores[ctr] << "\n";
   return;
}



PreviousNext

Related