Write a program that dynamically allocates an array large enough to hold a user-defined number of test scores (for one student). Do not accept negative numbers for test scores! Once all the scores are entered, the array should be passed to a function that drops the lowest score and calculates the average of the remaining scores. The result must be displayed in main. Your function must have the void return type (think about proper list of function parameters!).
Use pointer notations rather than subscripts whenever possible!
Here is code again:
I still trying work avg. but i need this code to work first:
#include <iostream>
usingnamespace std;
// void dropgrade (double *[], int);
// pass pointer to the first element of the array, and its size
int dropgrade ( double score[], int size ); // return new size
void calaverage ( constdouble* );
int main ()
{
cout << "Welcome" << endl;
double *score;
int n_scores;
cout << "How many test score would you like to enter ? " << endl;
cin >> n_scores;
while (n_scores <= 0)
{
cout << "Please enter number that greater than 0! " << endl;
cout << "How many test score would you like to enter ? " << endl;
cin >> n_scores;
}
score = newdouble [n_scores];
cout << "Enter the test score below" << endl;
cout << endl;
for (int index = 0; index < n_scores; index ++)
{
cout << "Test number " << (index + 1) << ": " << endl;
cin >> score [index];
if (score [index] <= 0)
{
cout << "The test number " << (index + 1) << " : is invalid" << endl;
cout << "Please enter only 0 or more!" << endl;
cin >> score [index];
}
}
std::cout << "scores as entered:\n" ;
for( int i = 0; i < n_scores; ++i ) std::cout << score[i] << ' ' ;
std::cout << '\n' ;
//dropgrade(&score, n_scores);
if( n_scores > 1 ) n_scores = dropgrade( score, n_scores );
std::cout << "after dropping the lowest score:\n" ;
for( int i = 0; i < n_scores; ++i ) std::cout << score[i] << ' ' ;
std::cout << '\n' ;
delete [] score;
}
// return the position of the lowest score in the array (or -1 if empty)
int position_of_lowest_score( constdouble score[] , int size )
{
if( size > 0 )
{
int pos_lowest = 0 ;
double lowest_score = score[0] ;
for( int i = 1 ; i < size ; ++i )
{
if( score[i] < lowest_score )
{
lowest_score = score[i] ;
pos_lowest = i ;
}
}
return pos_lowest ;
}
return -1 ;
}
// drop the lowest score and return the new size
int dropgrade( double score[] , int size)
{
if( size > 1 ) // at lest two scores
{
int pos_lowest = position_of_lowest_score( score, size ) ;
// drop the lowest score by moving the score in the last position
// to the position that holds the lowest score
score[pos_lowest] = score[size-1] ;
return size-1 ; // number of scores is one less after dropping the lowest
}
return size ; // nothing was dropped
}
TODO: Use pointer notations rather than subscripts whenever possible!