[Solved] C++ program functions arrays [closed]


I would declare the functions the following way

const size_t SCORE_NUM = 5;
const size_t STUDENT_NUM = 100; // at least not less than the number of records in the file

size_t inputScores( std::ifstream &, std::string[], double[][SCORE_NUM], size_t );

double computeAverage( const double[], size_t );

char computeLetterGrade( double );

void printGrades( const std::string[], const double[][SCORE_NUM], size_t );

For example function computeAverage could be defined as

double computeAverage( const double scores[], size_t n )
{
   double sum = 0.0;

   for ( size_t i = 0; i < n; i++ ) sum += scores[i];

   return ( n == 0 ? sum : sum / n );
}

It should be called for each row of the array of doubles.

The definition of function inputScores could look as (without testing):

size_t inputScores( std::ifstream &in, 
                    std::string students[], 
                    double scores[][SCORE_NUM], 
                    size_t n )
{
   std::string record;
   size_t i = 0;

   while ( i < n && std::getline( in, record ) )
   {
      std::istringstream is( record );

      std::string lastName;

      if ( is >> students[i] && is >> lastName )
      {
         students[i] += ' ';
         students[i] += lastName;
      }

      std::memset( scores[i], 0, SCORE_NUM * sizeof( double ) );

      size_t j = 0;

      while ( j < SCORE_NUM && is >> scores[i][j] ) ++j; 

      ++i;
   }

   return i;
}

8

solved C++ program functions arrays [closed]