You are here:

C++/Test score

Advertisement


Question
hi i have a homework that ask to write a program that dynamically allocates an array large enough to hold a user defined number of test scores and here is what i got so far

#include <iostream>
#include <iomanip>
using namespace std;

{
const int maxNumScores = 10;   
int getNumScores();
void getScores(double[], int);  
double average(double[], int);  
int numA(double[], int);       


int main()
{
   // Get the number of test scores
   int number_of_tests = getNumScores();

   // Now ... dynamically allocate an array large enough to hold all the scores
   double* scores = new double[number_of_tests];

   // ... and get the score for each test
   // Note: array of scores passed in, (and the updated array returned), by 'ref'
   getScores(scores, number_of_tests);

   // Display results ...
   cout << "The average of all " << number_of_tests << " test scores is: "
        << fixed << showpoint << setprecision(2)
        << average(scores, number_of_tests)
        << "\nThe number of A's is: "
        << numA(scores, number_of_tests) << endl << endl;

   // Free dynamically allocated array in memory
   delete [] scores;
   // Make scores point to null
   scores = NULL;

   system("pause");
}

// uses Global 'maxNumScores'
int getNumScores()
{
   int num;
   cout << "\nHow many test scores do you want to enter  : ";
   for(;;) // loop forever  ... until return
   {
       cin >> num;

       if( cin.good() )
       {
         cin.sync();  // flush cin
         // allow only integers in range 1..maxNumScores
         if(num > 0 && num <= maxNumScores)
         return num; // since we have a good number ...
       }
       else // in case !cin.good()
       {
         cin.clear();
         cin.sync();  // flush cin
       }
       cout << "Please enter an integer in the range 0.."
         << maxNumScores << " : ";
   }
}

void getScores(double score[], int numScores)
{
   cout << "Please enter the score for each test ...\n";
   for(int count=0; count<numScores; ++count)
   {
       cout << "Test " << setw(2) << (count+1) << ": ";
       // Input validation. Only numbers in range 0..100
       for(;;) // loop forever ... until break
       {
         cin >> score[count];
         if( cin.good() )
         {
         cin.sync(); // 'flush' cin ...
         if( score[count] >= 0 && score[count] <= 100)
         break; // out of inner for loop ... since this score is good
         }
         else
         {
         cin.clear(); // in case !cin.good() ...
         cin.sync(); // 'flush' cin ...
         }
         cout << "Please enter a score in the range 0..100: ";
       }
   }
}

double average(double score[], int numScores)
{
   if(numScores==0) return 0;

   double total = 0.0;
   // if we reach here, calculate the average score
   for(int i=0; i<numScores; ++i)
       total += score[i];

   return total/numScores;
}

int numA(double score[], int numScores)
{
   int count =0;
   for(int i=0; i<numScores; ++i)
       if (score[i] >= 90.0) ++count;

   return count;
}




but i was also asked to drop the lowest score and it should not be included in the calculation of the average. im having trouble on this part.

Your help would be greatly appreciated.. THANKS!!

Answer
Write a function to sum up all the scores:

double sum( const double score[], int numScores )
{
       double s = 0 ;
       for( int i = 0 ; i < numScores ; ++i ) s += score[i] ;
       return s ;
}


Another to determine the lowest score:

double lowest_score( const double score[], int numScores )
{
       double low = score[0] ;
       for( int i = 1 ; i < numScores ; ++i )
         if( score[i] < low ) low = score[i] ;
       return low ;
}


Now, put them together:

double drop_low_score_average( const double score[], int numScores )
{
       if( numScores < 2 ) return 0 ;
       double total = sum( score, numScores ) - lowest_score( score, numScores ) ;
       return total / (numScores-1) ;
}

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.

Experience

about 15 years or so

Education/Credentials
post graduate engineer

©2016 About.com. All rights reserved.