以下是我的代碼:它運行良好,但是如何使用戶輸入的平均分數達到平均值,然後在計算之前降低最低分數?我插入了一些代碼,但我相信它是錯誤的,因爲我無法讓它正常工作。有沒有更簡單的方法來編寫代碼?或者寫出最好的方法?謝謝。如何讓我的程序計算平均下降的最低分數?
#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;
}
*/
什麼問題,你用你的代碼有? – Xymostech
排序對於找到最低值似乎有點矯枉過正。將分數更改爲矢量然後執行此操作: double minScore = 1e6; int minScoreIndex; (count = 0; count score [count]){minScore = score [count]; minScoreIndex =計數; score.erase(score.begin()+ minScoreIndex);}} score.erase(score.begin()+ minScoreIndex); –
DigitalGhost
我需要讓它降低最低分,然後計算它。現在我只能用它來計算總分數而不降低最低分數。我有點不清楚如何適當地編寫函數來降低最低分數。我寫下了一些東西(我在底部註釋過),但我根本沒有那麼做。我可以使用像voidFindLowest ...然後average = total /(numTest - 最低)?或者我必須有一個排序數組,然後找到最低,然後放下它並顯示它? – user1787078