以下は私のコードです:それはうまく動作しますが、ユーザーが入力したすべてのスコアを平均化して、計算する前に最低のスコアを落とすにはどうすればよいですか?いくつかのコードを挿入しましたが、正しく機能させることができないため、間違っていると思います。そのコードを書くためのより短く、最も簡単な方法はありますか?またはそれを書くための最良の方法は?ありがとうございました。
#include <iostream>
#include <iomanip>
using namespace std;
//void sortArray(double arr[], int numTest, int scoreNum);
//void displayArray(double arr[], int numTest, int scoreNum);
int main()
{
//To dynamically allocate an array, Accumulator, to hold the average scores.
double *score;
double total = 0;
double average;
//int for counter, to hold the number of test scores.
int count;
int numTest;
// To obtain the number of test scores the user would like to enter.
cout << "How many test scores would you like to enter? " << endl;
cin >> numTest;
//Dynamically allocates an array large enough to hold the amount of test scores to enter.
score = new double[numTest];
//Get the test scores.
cout << "Enter the test score desired. " << endl;
for (count = 0; count < numTest; count++)
{
cout << "Score " << (count + 1) << ": ";
cin >> score[count];
}
//sortArray(score, numTest, scoreNum); (Need to get this part to work)
//displayArray( score, numTest, scoreNum); (Need to get this part to work)
//Calculate the total test scores.
for (count = 0; count < numTest; count++)
{
total += score[count];
}
//Calculate the test scores average minus the lowest score. (I need help here - is this how I drop the lowest score?)
average = total / numTest;
//Display the results
cout << fixed << showpoint << setprecision(2);
cout << "Test Scores Average with the lowest dropped is: " << average << endl;
//Free dynamically allocated memory
delete [] score;
score = 0; // Makes score point to null.
system("pause");
return 0;
}
/* void sortArray(double arr[], int numTest, int scoreNum)
{
double num = 0;
int posNum = 0;
for (int i = 0; i < numTest; i++)
{
for (int x = (i + 1); x < numTest; x++)
{
if ( arr[i] > arr[x] )
{
num = score[i];
posNum = scoreNum[i];
arr[i] = arr[x];
scoreNum[i] = scoreNum[x];
arr[x] = num;
scoreNum[x] = posNum;
}
}
}
}
void displayArray(double arr[], int numTest, int scoreNum)
{
double average = 0.0;
double sum = 0.0;
int x;
for (int i = 0; i < numTest; i++)
{
for (x = 1; x < numTest; x++)
{
sum += arr[x];
}
}
average = sum(numTest - 1);
cout << fixed << showpoint << setprecision(2) << endl;
cout << "The average of all test scores dropping the lowest is: " << average << endl;
}
*/