Define array size with const - C++ Data Type

C++ examples for Data Type:Array

Description

Define array size with const

Demo Code

#include <iostream>
using namespace std;
#include <iomanip>
void pr_scores(float scores[]);
const int CLASS_NUM = 6;  // Constant holds array size.
void main()/*w ww  .  java  2s. c o m*/
{
   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;
}

Related Tutorials